简体   繁体   English

圆形动画不起作用

[英]Circle Animation doesn't work

I'm drawing a loading circle in a view. 我正在视图中绘制一个加载圆圈。 If I directly open the view with Is initial View Controller. 如果我直接使用Is Initial View Controller打开视图。 Circle is animating but If I choose to open it from previous view (tableview). Circle正在设置动画,但是如果我选择从上一个视图(tableview)打开它。 It comes with draw but doesn't animating. 它带有抽签,但没有动画。 I tried to call setup(), didawake functions in viewdidload but still no chance. 我试图在viewdidload中调用setup()和didawake函数,但还是没有机会。 It doesn't working. 它不起作用。

class SpinningView: UIView {

    let circleLayer = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

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

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    @IBInspectable var lineWidth: CGFloat = 4 {
        didSet {
            circleLayer.lineWidth = lineWidth
            setNeedsLayout()
        }
    }

    func setup() {
        circleLayer.lineWidth = lineWidth
        circleLayer.lineWidth = 10
        circleLayer.fillColor = nil
        circleLayer.strokeColor = UIColor(red: 0.8078, green: 0.2549, blue: 0.2392, alpha: 1.0).cgColor
        layer.addSublayer(circleLayer)
        tintColorDidChange()
        updateAnimation()
    }
    func updateAnimation() {
        if animating {
            circleLayer.add(strokeEndAnimation, forKey: "strokeEnd")
            circleLayer.add(strokeStartAnimation, forKey: "strokeStart")
        }
        else {
            circleLayer.removeAnimation(forKey: "strokeEnd")
            circleLayer.removeAnimation(forKey: "strokeStart")
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = min(bounds.width, bounds.height) / 2 - circleLayer.lineWidth/2

        let startAngle = CGFloat(-M_PI_2)
        let endAngle = startAngle + CGFloat(M_PI * 2)
        let path = UIBezierPath(arcCenter: CGPoint.zero, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

        circleLayer.position = center
        circleLayer.path = path.cgPath
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }
    override func tintColorDidChange() {
        super.tintColorDidChange()
        circleLayer.strokeColor = tintColor.cgColor
    }
    @IBInspectable var animating: Bool = true {
        didSet {
            updateAnimation()
        }
    }

    let strokeEndAnimation: CAAnimation = {
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = 2
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        let group = CAAnimationGroup()
        group.duration = 2.5
        group.repeatCount = MAXFLOAT
        group.animations = [animation]

        return group
    }()

    let strokeStartAnimation: CAAnimation = {
        let animation = CABasicAnimation(keyPath: "strokeStart")
        animation.beginTime = 0.5
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = 2
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        let group = CAAnimationGroup()
        group.duration = 2.5
        group.repeatCount = MAXFLOAT
        group.animations = [animation]

        return group
    }()


}

I fixed the problem by calling awakefromNib() function in viewdidappear. 我通过在viewdidappear中调用awakefromNib()函数解决了该问题。 So my code creates a circle that animates like googles circles. 因此,我的代码创建了一个类似于Google圈子的动画圈子。

override func viewDidAppear(_ animated: Bool) {
    mSpinningView.awakeFromNib()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM