I created a custom UIView
subclass. In it, I created a CAShapeLayer
as a mask for a CAGradientLayer
. I am using Storyboards and with @IBDesignable
everything looks ok in the Storyboard. This is how it looks which is perfect:
When I run the app in the Simulator, it does not appear ok:
This is the code which sets this up:
@IBDesignable
class ProgressView: UIView {
let progressLayer = CAShapeLayer()
let gradientLayer = CAGradientLayer()
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
func setup() {
// Setup progress layer.
progressLayer.lineWidth = 3.0
progressLayer.fillColor = nil
progressLayer.strokeEnd = 1
progressLayer.strokeColor = UIColor.blackColor().CGColor
progressLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
let radius = CGFloat(self.frame.height / 2.5)
let startAngle = CGFloat(-M_PI / 2)
let endAngle = CGFloat(3 * M_PI / 2)
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
progressLayer.path = path.CGPath
layer.addSublayer(progressLayer)
// Setup gradient layer mask.
gradientLayer.colors = [Constants.ColorPalette.teal.CGColor, Constants.ColorPalette.orange.CGColor, Constants.ColorPalette.pink.CGColor]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
gradientLayer.frame = self.frame
gradientLayer.mask = progressLayer
layer.addSublayer(gradientLayer)
}
.....
I've tried changing the frames
and using awakeFromNib
and required init?(coder aDecoder: NSCoder)
to setup the view but it does not seem to work. I want to do everything within the UIView
subclass and not a UIViewController
.
Any ideas?