我的指标是空白的

时间:2017-02-19 00:48:57

标签: ios swift core-animation

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

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.
}


  }

1 个答案:

答案 0 :(得分:-1)

持有view的{​​{1}}只是浮动,感到迷茫,感到不满意不属于,而不是已添加给某人。 :)

编辑: 好的约翰,现在我们被困住了,让我们将指示符添加到某人

indicator