Ios 我的指示灯是空的

Ios 我的指示灯是空的,ios,swift,core-animation,Ios,Swift,Core Animation,我使用自定义指示器,但当我在视图中调用子类指示器时,我的视图控制器为空,但当我在操场上运行它时,我可以在侧窗中看到它。这是指示器的代码。没有错误,但我的指示灯没有显示。这是我的问题。如果有人能告诉我原因,我将不胜感激 import UIKit class MaterialLoadingIndicator: UIView { let MinStrokeLength: CGFloat = 0.05 let MaxStrokeLength: CGFloat = 0.7 let circleShap

我使用自定义指示器,但当我在视图中调用子类指示器时,我的视图控制器为空,但当我在操场上运行它时,我可以在侧窗中看到它。这是指示器的代码。没有错误,但我的指示灯没有显示。这是我的问题。如果有人能告诉我原因,我将不胜感激

import UIKit

class MaterialLoadingIndicator: UIView {

let MinStrokeLength: CGFloat = 0.05
let MaxStrokeLength: CGFloat = 0.7
let circleShapeLayer = CAShapeLayer()

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    initShapeLayer()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func initShapeLayer() {
    circleShapeLayer.actions = ["strokeEnd" : NSNull(),
                                "strokeStart" : NSNull(),
                                "transform" : NSNull(),
                                "strokeColor" : NSNull()]
    circleShapeLayer.backgroundColor = UIColor.clear.cgColor
    circleShapeLayer.strokeColor     = UIColor.blue.cgColor
    circleShapeLayer.fillColor       = UIColor.clear.cgColor
    circleShapeLayer.lineWidth       = 5
    circleShapeLayer.lineCap         = kCALineCapRound
    circleShapeLayer.strokeStart     = 0
    circleShapeLayer.strokeEnd       = MinStrokeLength
    let center                       = CGPoint(x: bounds.width*0.5, y: bounds.height*0.5)
    circleShapeLayer.frame           = bounds
    circleShapeLayer.path            = UIBezierPath(arcCenter: center,
                                                    radius: center.x,
                                                    startAngle: 0,
                                                    endAngle: CGFloat(M_PI*2),
                                                    clockwise: true).cgPath
    layer.addSublayer(circleShapeLayer)
}

func startAnimating() {
    if layer.animation(forKey: "rotation") == nil {
        startColorAnimation()
        startStrokeAnimation()
        startRotatingAnimation()
    }
}

private func startColorAnimation() {
    let color      = CAKeyframeAnimation(keyPath: "strokeColor")
    color.duration = 10.0
    color.values   = [UIColor(hex: 0x4285F4, alpha: 1.0).cgColor,
                      UIColor(hex: 0xDE3E35, alpha: 1.0).cgColor,
                      UIColor(hex: 0xF7C223, alpha: 1.0).cgColor,
                      UIColor(hex: 0x1B9A59, alpha: 1.0).cgColor,
                      UIColor(hex: 0x4285F4, alpha: 1.0).cgColor]
    color.calculationMode = kCAAnimationPaced
    color.repeatCount     = Float.infinity
    circleShapeLayer.add(color, forKey: "color")
}

private func startRotatingAnimation() {
    let rotation            = CABasicAnimation(keyPath: "transform.rotation.z")
    rotation.toValue        = M_PI*2
    rotation.duration       = 2.2
    rotation.isCumulative     = true
    rotation.isAdditive       = true
    rotation.repeatCount    = Float.infinity
    layer.add(rotation, forKey: "rotation")
}

private func startStrokeAnimation() {
    let easeInOutSineTimingFunc = CAMediaTimingFunction(controlPoints: 0.39, 0.575, 0.565, 1.0)
    let progress: CGFloat     = MaxStrokeLength
    let endFromValue: CGFloat = circleShapeLayer.strokeEnd
    let endToValue: CGFloat   = endFromValue + progress
    let strokeEnd                   = CABasicAnimation(keyPath: "strokeEnd")
    strokeEnd.fromValue             = endFromValue
    strokeEnd.toValue               = endToValue
    strokeEnd.duration              = 0.5
    strokeEnd.fillMode              = kCAFillModeForwards
    strokeEnd.timingFunction        = easeInOutSineTimingFunc
    strokeEnd.beginTime             = 0.1
    strokeEnd.isRemovedOnCompletion   = false
    let startFromValue: CGFloat     = circleShapeLayer.strokeStart
    let startToValue: CGFloat       = fabs(endToValue - MinStrokeLength)
    let strokeStart                 = CABasicAnimation(keyPath: "strokeStart")
    strokeStart.fromValue           = startFromValue
    strokeStart.toValue             = startToValue
    strokeStart.duration            = 0.4
    strokeStart.fillMode            = kCAFillModeForwards
    strokeStart.timingFunction      = easeInOutSineTimingFunc
    strokeStart.beginTime           = strokeEnd.beginTime + strokeEnd.duration + 0.2
    strokeStart.isRemovedOnCompletion = false
    let pathAnim                 = CAAnimationGroup()
    pathAnim.animations          = [strokeEnd, strokeStart]
    pathAnim.duration            = strokeStart.beginTime + strokeStart.duration
    pathAnim.fillMode            = kCAFillModeForwards
    pathAnim.isRemovedOnCompletion = false
    CATransaction.begin()
    CATransaction.setCompletionBlock {
        if self.circleShapeLayer.animation(forKey: "stroke") != nil {
            self.circleShapeLayer.transform = CATransform3DRotate(self.circleShapeLayer.transform, CGFloat(M_PI*2) * progress, 0, 0, 1)
            self.circleShapeLayer.removeAnimation(forKey: "stroke")
            self.startStrokeAnimation()
        }
    }
    circleShapeLayer.add(pathAnim, forKey: "stroke")
    CATransaction.commit()
}

func stopAnimating() {
    circleShapeLayer.removeAllAnimations()
    layer.removeAllAnimations()
    circleShapeLayer.transform = CATransform3DIdentity
    layer.transform            = CATransform3DIdentity
}

 }

 extension UIColor {

convenience init(hex: UInt, alpha: CGFloat) {
    self.init(
        red: CGFloat((hex & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((hex & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(hex & 0x0000FF) / 255.0,
        alpha: CGFloat(alpha)
    )
}

}
下面是viewdidload中我的视图控制器的代码

import UIKit

 class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let view      = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
    let indicator = MaterialLoadingIndicator(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    indicator.center = CGPoint(x: 320*0.5, y: 568*0.5)
    view.addSubview(indicator)
    indicator.startAnimating()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


  }
拿着指示器的视图只是四处飘荡,感觉失落,因为不属于某人而感到不高兴,没有被添加到某人身上 编辑: 好了,约翰,现在我们被卡住了,让我们给某人添加一个指示器


我想把这个放进牢房,但不是working@john你们是怎么尝试的?我把它放在了索引行的单元格中,但加载时间太长了
override func viewDidLoad() {
    super.viewDidLoad()
    let view      = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
    let indicator = MaterialLoadingIndicator(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    indicator.center = CGPoint(x: 320*0.5, y: 568*0.5)
    view.addSubview(indicator)
    indicator.startAnimating()

    self.view.addSubview(view) // John, this is what was missing
}