圈层错误 super.init - Swift

Circle Layer error with super.init - Swift

这将是一个非常基本的问题。

我正在研究这个答案: Animate drawing of a circle

但无论我如何格式化它,我都会收到错误消息。我可以从错误中看出我没有初始化圆圈,我确信这只是一个定位问题,但不确定如何正确执行此操作或者我的布局有什么问题。

当我这样尝试时,出现错误('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")
    }

}

然后像这样尝试将它移动到初始化程序之后,这不会给我一个错误):

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")
        }

    }

但是当我尝试在我的 viewcontroller.swift 中放置一个引用 circleLayer 的函数时,我得到了未解析的标识符:

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")
}      

我确定这只是一些非常简单的事情,但我不确定是什么。

感谢您的帮助。

来自文档

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.

在声明行中初始化circleLayer并移动self.backgroundColor = ...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")
  }
  
}