A dynamic framework that allows you to easily add shadows to a UIView instance, using the same shadow paramters in Sketch.
This framework even supports animations on showing / hiding or changing the shadow.
This project is on a unstable beta stage. Think twice before using it on a business project.
Compatible with Swift 4.2 + and iOS 12.0 +.
Make sure you have Carthage installed. Otherwise, simply install with Homebrew:
brew install carthage
- At the directory of your
.xcodeproj
or.xcworkspace
file, create a file namedCartfile
. Open the file using your text editor, add one line:
github "EdgarDegas/ALayout"
- At the same directory, execute this line into your Terminal:
carthage update
-
When
update
is done, a new folderCarthage
is created. -
You need to add Run Script to at your build phases:
Make sure the path in the
Input Files
section is correct.
- Import ALayout
import ALayout
- Initialize a
Shadow
object:
Shadow(color:opacity:Offset:blur:spread:)
The parameters are basically the same with those in Sketch. In case you are not familiar with UI design:
- color:
ShadowColor
, UIColoris OK. Or you can use
ShadowRGBAColor,
ShadowHexColor` for convenience color setup. - opacity:
CGFloat
, ranging from 0.0 (transparent) to 1.0 (opaque) - offset, a tuple containing two CGFloat values:
- dx:
CGFloat
, offset on x-axis, negative (leftward) or positive (rightward) - dy:
CGFloat
, offset on x-axis, negative (upward) or positive (downward)
- dx:
- blur:
CGFloat
, the blur radius of shadow, positive value - spread:
CGFloat
, increase or decrese the size of shadow from the size of your view
Initializing example:
let myShadow = Shadow(color: UIColor.black, opacity: 1, offset: (dx: 0, dy: 0), blur: 24)
- Set shadow:
myView.set(shadow: myShadow, animated: true)
And if you set a different Shadow
instance now, you would see the shadow changing animation.
myView.set(shadow: anotherShadow, aniamted: true)
Somtimes you may need to specify
clipsToBounds
tofalse
:
myView.clipsToBounds = false
- Remove shadow:
myView.removeShadow(animated: true)
- If your user interface needs layout, you should call this method at a proper timing:
myView.layoutShadow()
By calling set(shadow:)
on UIView instances, the framework inserts a CALayer with shadow at the bottom of the sublayers of the view's layer.