# FreeShadow **Repository Path**: kmyhy/free-shadow ## Basic Information - **Project Name**: FreeShadow - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-13 - **Last Updated**: 2021-09-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FreeShadow ## What is FreeShadow In iOS development, drawing shadow is a common task. But most of the time, we only draw these shadow as a rectangle shape. How about draw a shadow from a irregular shape? This is why FreeShadow comes up. By using FreeShadow, you can draw shadows from UIBezierPath which is from any irregular geometrical shapes even a image. ## How to use it Copy the ShadowView.swift into your own project. ShadowView is a protocol which includes 3 properties: ```swift var shadow: NSShadow{ get set } var shadowPath: UIBezierPath { get set } var intrinsicSize: CGSize { get set } ``` First of all, you need to implement these properties in your own customed UIView: ```swift @IBDesignable class BundleShadowView: UIView, ShadowView { lazy var shadow: NSShadow = { // 1 let shadow = NSShadow() shadow.shadowColor = UIColor.lightGray shadow.shadowOffset = CGSize(width: 0, height: 0) shadow.shadowBlurRadius = 15 return shadow }() lazy var shadowPath: UIBezierPath = { // 2 let bezierPath = UIBezierPath() bezierPath.move(to: CGPoint(x: 146, y: 117.98)) ... ... bezierPath.close() return bezierPath }() var intrinsicSize: CGSize = CGSize(width: 278, height: 401) // 3 ``` 1. You must specify a NSShadow which will tell FreeShadow how to draw the shadow such as shadow's color, offset or radius. 2. Then you need to tell FreeShadow what the shadow's shape look like which is an UIBesierPath. You definitely can build the path from scratch, but from my point, the simplest way to do it is use PaintCode. 3. intrinsicSize is the mininal frame size to show the shadow intactly. The next, you are required to overwrite the draw(_ rect:) method. But fortunately, you don't need to write a batch of stereotyped code in this method other than call ShadowView's drawShadow(rect) method. ```swift override func draw(_ rect: CGRect) { drawShadow(rect) } ``` After that, let's move on to the Storyboard. In your Storyboard, drag a UIImage into view controller's view and set up its autolayout as your please. It's an image with a bottle and a box together. we'll take this as a example of drawing irregular shape's shadow: Then drag an UIView into canvas from Library. Change its class to BundleShadowView. Bear a few seconds with Storyboard to redraw the canvas, and the bottle shape shadow will show: Move the BundleShadowView under the UIImage in order to make these two aligned and add appropiate constraints to it: The last but not least is add this line in ViewController's viewDidLoad() method: ``` view.insertSubview(bottleBottomShadowView, at: 0) ``` This make the shodow would always be under the bottle image. Build & Run , Bingo!