From abce7cfca50d3810ee9edfe04b6277155c588201 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 3 Aug 2020 13:53:59 -0400 Subject: [PATCH] conic gradient --- MVMCoreUI/Atomic/Atoms/Views/Wheel.swift | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Views/Wheel.swift b/MVMCoreUI/Atomic/Atoms/Views/Wheel.swift index da71ea0f..430aad94 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Wheel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Wheel.swift @@ -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