Search code examples
iosswiftuiviewinitializer

Circle Layer error with super.init - Swift


This is going to be a really basic question.

I am working through this answer: Animate drawing of a circle

But no matter how I format it I get an error. I can see from the error that I have not initialized circle and I am sure it is just a positioning thing but not sure what or how to do that correctly or what is wrong with how I have layout.

When I try like this i get the error ('self.circleLayer' not initialized at super.init call):

import UIKit

class CircleView: UIView {

    let circleLayer: CAShapeLayer!

        override init(frame: CGRect) {
            super.init(frame: frame)


            self.backgroundColor = UIColor.clearColor()

            // Use UIBezierPath as an easy way to create the CGPath for the layer.
            // The path should be the entire circle.
            let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)

            // Setup the CAShapeLayer with the path, colors, and line width
            circleLayer = CAShapeLayer()
            circleLayer.path = circlePath.CGPath
            circleLayer.fillColor = UIColor.clearColor().CGColor
            circleLayer.strokeColor = UIColor.redColor().CGColor
            circleLayer.lineWidth = 5.0;

            // Don't draw the circle initially
            circleLayer.strokeEnd = 0.0

            // Add the circleLayer to the view's layer's sublayers
            layer.addSublayer(circleLayer)
        }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Then tried moving it to after the initializer like this which doesn't give me an error):

import UIKit

    class CircleView: UIView {

            override init(frame: CGRect) {
                super.init(frame: frame)

                let circleLayer: CAShapeLayer!
                self.backgroundColor = UIColor.clearColor()

                // Use UIBezierPath as an easy way to create the CGPath for the layer.
                // The path should be the entire circle.
                let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)

                // Setup the CAShapeLayer with the path, colors, and line width
                circleLayer = CAShapeLayer()
                circleLayer.path = circlePath.CGPath
                circleLayer.fillColor = UIColor.clearColor().CGColor
                circleLayer.strokeColor = UIColor.redColor().CGColor
                circleLayer.lineWidth = 5.0;

                // Don't draw the circle initially
                circleLayer.strokeEnd = 0.0

                // Add the circleLayer to the view's layer's sublayers
                layer.addSublayer(circleLayer)
            }


        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

    }

But then when I try to put a function in my viewcontroller.swift that references circleLayer I get unresolved identifier:

func animateCircle(duration: NSTimeInterval) {
    // We want to animate the strokeEnd property of the circleLayer
    let animation = CABasicAnimation(keyPath: "strokeEnd")

    // Set the animation duration appropriately
    animation.duration = duration

    // Animate from 0 (no circle) to 1 (full circle)
    animation.fromValue = 0
    animation.toValue = 1

    // Do a linear animation (i.e. the speed of the animation stays the same)
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
    // right value when the animation ends.
    circleLayer.strokeEnd = 1.0

    // Do the actual animation
    circleLayer.addAnimation(animation, forKey: "animateCircle")
}      

I am sure it just something really simple but I am not sure what.

Thanks for your help.


Solution

  • From the documentation

    Safety check 1

    A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.

    Initialize circleLayer in the declaration line and move self.backgroundColor = ... after super.init

    class CircleView: UIView {
      
      let circleLayer = CAShapeLayer()
      
      override init(frame: CGRect) {
        
        // Use UIBezierPath as an easy way to create the CGPath for the layer.
        // The path should be the entire circle.
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)
        
        super.init(frame: frame)
        // Setup the CAShapeLayer with the path, colors, and line width
        
        self.backgroundColor = UIColor.clearColor()
        circleLayer.path = circlePath.CGPath
        circleLayer.fillColor = UIColor.clearColor().CGColor
        circleLayer.strokeColor = UIColor.redColor().CGColor
        circleLayer.lineWidth = 5.0;
        
        // Don't draw the circle initially
        circleLayer.strokeEnd = 0.0
        
        // Add the circleLayer to the view's layer's sublayers
        layer.addSublayer(circleLayer)
      }
      
      
      required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
      
    }