Last active
April 12, 2017 09:11
Star
You must be signed in to star a gist
Custom UIView hosting a CAShapeLayer
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
// 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