Skip to content

Instantly share code, notes, and snippets.

@algal
Last active April 12, 2017 09:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save algal/2379b17a6671c5dd3980b9b41641768b to your computer and use it in GitHub Desktop.
Custom UIView hosting a CAShapeLayer
// 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