Skip to content

Instantly share code, notes, and snippets.

@algal algal/CircleView.swift

Last active Apr 12, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.