Ios 使用super.init-Swift循环层错误

Ios 使用super.init-Swift循环层错误,ios,swift,uiview,initializer,Ios,Swift,Uiview,Initializer,这将是一个非常基本的问题 我正在研究这个答案: 但无论我如何格式化它,我都会得到一个错误。我可以从错误中看出,我没有初始化圆,我确定这只是一个定位问题,但不确定正确地做什么或如何做,或者我的布局有什么问题 当我这样尝试时,我得到了错误('self.circleLayer'未在super.init调用中初始化): 然后尝试将其移动到初始值设定项之后,如下所示,这不会给我一个错误): 但是,当我尝试将引用circleLayer的函数放入我的viewcontroller.swift时,我会得到未解析

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

我正在研究这个答案:

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

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

然后尝试将其移动到初始值设定项之后,如下所示,这不会给我一个错误):

但是,当我尝试将引用circleLayer的函数放入我的viewcontroller.swift时,我会得到未解析的标识符:

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")
  }
  
}
从文件中

安全检查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")
  }
  
}

非常感谢!所以我假设在mu视图控制器中使用它,我只需要声明它类似于var circleLayer:circleLayer!为什么是可选的,虽然它显然是非可选的?非常感谢!所以我假设在mu视图控制器中使用它,我只需要声明它类似于var circleLayer:circleLayer!为什么是可选的,尽管它显然是非可选的?
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")
  }
  
}