Created
April 4, 2019 15:40
-
-
Save yukin01/c740f179f4e12be4591e988f8420d024 to your computer and use it in GitHub Desktop.
ClipToRoundView
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 | |
final class ClipToRoundView: UIView { | |
@IBInspectable | |
var fillColor: UIColor = .white { | |
didSet { | |
shapeLayer.fillColor = fillColor.cgColor | |
} | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
setPath() | |
} | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
setPath() | |
} | |
override class var layerClass: AnyClass { | |
return CAShapeLayer.self | |
} | |
private var shapeLayer: CAShapeLayer { | |
guard let shapeLayer = self.layer as? CAShapeLayer else { fatalError() } | |
return shapeLayer | |
} | |
private func setPath() { | |
shapeLayer.fillColor = fillColor.cgColor | |
shapeLayer.fillRule = .evenOdd | |
let maskPath = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.height / 2) | |
let path = UIBezierPath(rect: bounds) | |
path.append(maskPath) | |
shapeLayer.path = path.cgPath | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment