Last active
October 10, 2024 04:53
-
-
Save algal/2379b17a6671c5dd3980b9b41641768b to your computer and use it in GitHub Desktop.
Custom UIView hosting a CAShapeLayer
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
// known-good: Xcode 8.2.1, Swift 3, 2017-02-08 | |
/** | |
Draws a circle, centered in the view, with the specified radius | |
*/ | |
class CircleView: UIView | |
{ | |
// API | |
var circleRadius:CGFloat = 15 { | |
didSet { self.setNeedsLayout() } | |
} | |
var color:UIColor = .black { | |
didSet { self.shapeLayer.fillColor = color.cgColor } | |
} | |
override static var layerClass:AnyClass { | |
return CAShapeLayer.self | |
} | |
private var shapeLayer:CAShapeLayer { | |
return self.layer as! CAShapeLayer | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
self.shapeLayer.path = self.circlePath() | |
} | |
private func circlePath() -> CGPath { | |
let center = CGPoint(x: self.layer.bounds.midX, y: self.layer.bounds.midY) | |
let circleRect = CGRect(origin: center, size: .zero).insetBy(dx: -1*circleRadius, dy: -1*circleRadius) | |
let satelittePath = UIBezierPath(ovalIn: circleRect).cgPath | |
return satelittePath | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment