圆层错误与超级.init - Swift



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

我正在研究这个答案:动画绘制一个圆

但无论我如何格式化,我得到一个错误。我可以从错误中看到,我没有初始化圆圈,我确信这只是一个定位的东西,但不确定什么或如何正确地做到这一点,或者我的布局有什么问题。

当我这样尝试时,我得到错误('self。circleLayer'未在super初始化。init调用):

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

我确信它只是一些非常简单的东西,但我不确定是什么。

谢谢你的帮助。

从文档

安全检查1

指定初始化式必须确保所有的由其类引入的属性在委托之前被初始化到一个超类初始化器

初始化声明行中的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")
  }
  
}

最新更新