Created
June 7, 2019 08:03
-
-
Save reedom/77044342b5221db93c91384d2652791a to your computer and use it in GitHub Desktop.
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
| open class CircleShapeView: UIView { | |
| override open var backgroundColor: UIColor? { | |
| get { | |
| guard | |
| let layer = layer as? CAShapeLayer, | |
| let cgColor = layer.fillColor | |
| else { return super.backgroundColor } | |
| return UIColor(cgColor: cgColor) | |
| } | |
| set { | |
| super.backgroundColor = UIColor.clear | |
| let newColor = newValue ?? UIColor.clear | |
| if let layer = layer as? CAShapeLayer { | |
| layer.fillColor = newColor.cgColor | |
| layer.strokeColor = newColor.cgColor | |
| } | |
| } | |
| } | |
| override open class var layerClass: AnyClass { | |
| get { | |
| return CAShapeLayer.self | |
| } | |
| } | |
| public required init?(coder: NSCoder) { | |
| super.init(coder: coder) | |
| initShape() | |
| } | |
| public override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| initShape() | |
| } | |
| func initShape() { | |
| guard let layer = layer as? CAShapeLayer else { return } | |
| let path = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.width / 2).cgPath | |
| layer.path = path | |
| layer.fillRule = .nonZero | |
| } | |
| override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
| guard | |
| let layer = layer as? CAShapeLayer, | |
| let path = layer.path | |
| else { return nil } | |
| if path.contains(point, using: (layer.fillRule == .evenOdd ? .evenOdd : .winding)) { | |
| return super.hitTest(point, with:event) | |
| } else { | |
| return nil | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment