Last active
April 16, 2020 08:10
-
-
Save foxicode/952d7459a95a3fc0d54e1e0fb8c6e24f to your computer and use it in GitHub Desktop.
Swift UIKit shadows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UIView { | |
| @IBInspectable var dropShadow: Bool { | |
| set { | |
| layer.shadowOffset = CGSize(width: 0, height: 0) | |
| if newValue { | |
| updateShadow() | |
| } else { | |
| layer.shadowColor = UIColor.clear.cgColor | |
| layer.shadowOpacity = 0.0 | |
| layer.shadowRadius = 0 | |
| layer.shadowPath = nil | |
| layer.shouldRasterize = false | |
| } | |
| } | |
| get { | |
| layer.shadowOpacity > 0.0 && layer.shadowRadius > 0 | |
| } | |
| } | |
| func updateShadow() { | |
| layer.shadowColor = UIColor.black.cgColor | |
| layer.shadowOpacity = 0.5 | |
| layer.shadowRadius = 4 | |
| layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius).cgPath | |
| layer.shouldRasterize = true | |
| layer.rasterizationScale = UIScreen.main.scale | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UIView { | |
| func updateShadows() { | |
| if self.dropShadow { | |
| self.updateShadow() | |
| } | |
| subviews.forEach { | |
| $0.updateShadows() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment