ios - super.init 的 Circle Layer 错误 - Swift

标签 ios swift uiview initializer

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

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

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

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

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

感谢您的帮助。

最佳答案

来自文档

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

关于ios - super.init 的 Circle Layer 错误 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39226853/

相关文章:

ios - 使用相机的图像选择器未使用相机图像

ios - UIImage 在 self 调整 UITableViewCell 拉伸(stretch)

ios - 如何去除UISlider拇指周围的阴影

ios - 从同一个 AVCaptureSession 创建多个 AVCaptureVideoPreviewLayer (IOS Camera) 并将它们添加到同一层

ios - dispatch_async什么时候真正调度?

iphone - 如何显示可在 iOS 中打开 PDF 的外部应用程序列表?

ios - iCarousel numberOfItemsInCarousel

ios - 在 iPhone 中获取按钮标签值

ios - 如何使用 Swift 中的组替换字符串中的出现?

ios - UIView 不重绘,但元素错位