簡體   English   中英

使用super.init進行圓層錯誤-Swift

[英]Circle Layer error with super.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")
        }

    }

但是然后當我嘗試在引用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 ,並 super.init 之后移動self.backgroundColor = ...

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM