conic gradient
This commit is contained in:
parent
75702279ab
commit
abce7cfca5
@ -19,8 +19,6 @@ import UIKit
|
||||
// MARK: setup
|
||||
open override func setupView() {
|
||||
super.setupView()
|
||||
//avoid adding height constraint multiple times
|
||||
guard heightConstraint == nil else { return }
|
||||
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
|
||||
heightConstraint?.isActive = true
|
||||
widthAnchor.constraint(equalTo: heightAnchor).isActive = true
|
||||
@ -51,6 +49,31 @@ import UIKit
|
||||
}
|
||||
heightConstraint?.constant = graphObject.diameter
|
||||
|
||||
// iOS 12 uses the conic gradient and a mask for simplicity.
|
||||
if #available(iOS 12, *) {
|
||||
let gradient = CAGradientLayer()
|
||||
gradient.type = .conic
|
||||
gradient.startPoint = CGPoint(x: 0.5, y: 0.5)
|
||||
gradient.endPoint = CGPoint(x: 0.5, y: 0.0)
|
||||
gradient.frame = CGRect(x: 0, y: 0, width: graphObject.diameter, height: graphObject.diameter)
|
||||
gradient.colors = graphObject.colors.map({ (color) -> CGColor in
|
||||
return color.cgColor
|
||||
})
|
||||
gradientLayer = gradient
|
||||
layer.addSublayer(gradient)
|
||||
|
||||
let center = CGPoint(x: gradient.bounds.midX, y: gradient.bounds.midY)
|
||||
let radius = (graphObject.diameter - graphObject.lineWidth) / 2.0
|
||||
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: (3 / 2 * .pi), endAngle: -(1 / 2 * .pi), clockwise: false)
|
||||
let mask = CAShapeLayer()
|
||||
mask.fillColor = UIColor.clear.cgColor
|
||||
mask.strokeColor = UIColor.white.cgColor
|
||||
mask.lineWidth = graphObject.lineWidth
|
||||
mask.path = path.cgPath
|
||||
gradient.mask = mask
|
||||
return
|
||||
}
|
||||
|
||||
//create circle path
|
||||
let radius = graphObject.diameter / 2.0
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user