Created
November 13, 2019 06:13
-
-
Save amixpal/8f5ad0fa019cecc1f2f25a946a22823f to your computer and use it in GitHub Desktop.
This file contains 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
@IBDesignable | |
class RoundedView: UIView { | |
@IBInspectable | |
var topCornersRounded: Bool = false { | |
didSet { | |
if self.topCornersRounded { | |
layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] | |
} | |
} | |
} | |
@IBInspectable | |
var bottomCornersRounded: Bool = false { | |
didSet { | |
if self.bottomCornersRounded { | |
layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner] | |
} | |
} | |
} | |
@IBInspectable | |
var allCornerRounded: Bool = false { | |
didSet { | |
if self.allCornerRounded { | |
layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMinYCorner] | |
} | |
} | |
} | |
@IBInspectable | |
var cornerRad: CGFloat = 0 { | |
didSet { | |
layer.cornerRadius = cornerRad | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
clipsToBounds = true | |
topCornersRounded = false | |
bottomCornersRounded = false | |
allCornerRounded = false | |
} | |
private var theShadowLayer: CAShapeLayer? | |
} | |
/* | |
private var shadowLayer: CAShapeLayer! | |
private var cornerRadius: CGFloat = 25.0 | |
private var fillColor: UIColor = .blue // the color applied to the shadowLayer, rather than the view's backgroundColor | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
if shadowLayer == nil { | |
shadowLayer = CAShapeLayer() | |
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath | |
shadowLayer.fillColor = fillColor.cgColor | |
shadowLayer.shadowColor = UIColor.black.cgColor | |
shadowLayer.shadowPath = shadowLayer.path | |
shadowLayer.shadowOffset = CGSize(width: 0.0, height: 1.0) | |
shadowLayer.shadowOpacity = 0.2 | |
shadowLayer.shadowRadius = 3 | |
layer.insertSublayer(shadowLayer, at: 0) | |
} | |
} | |
view rawAddShadowAndRoundedCorners.swift hosted with :heart: by GitHub | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment