核心动画 - 组中动画的单独时间

时间:2017-02-21 12:38:52

标签: ios swift uiview core-animation

我有两个动画,我想链接成组。

他们看起来像这样:

func animate(circle: UIView) {

    var animations = [CABasicAnimation]()

    let opacityAnimation = CABasicAnimation(keyPath: "opacity")
    opacityAnimation.repeatCount = Float.infinity
    opacityAnimation.autoreverses = true
    opacityAnimation.fromValue = 0.0
    opacityAnimation.toValue = 0.40

    animations.append(opacityAnimation)

    let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
    scaleAnimation.repeatCount = Float.infinity
    scaleAnimation.autoreverses = true
    scaleAnimation.fromValue = 0.8
    scaleAnimation.toValue = 1

    animations.append(scaleAnimation)

    let group = CAAnimationGroup()
    group.duration = 1.4
    group.repeatCount = FLT_MAX
    group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    group.animations = animations

    circle.layer.add(group, forKey: nil)
}

有没有办法可以为第一个动画设置单独的持续时间,然后分成第二个动画?例如,opacityAnimation需要3秒,scaleAnimation需要1秒

我该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试使用此代码,但我没有测试此代码。

func animate(circle: UIView) {

        CATransaction.begin()
        CATransaction.setCompletionBlock({

            let scaleAnimate:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimate.fromValue = 1.0
            scaleAnimate.toValue = 0.0
            scaleAnimate.duration = 0.9
            scaleAnimate.repeatCount = 1
            scaleAnimate.removedOnCompletion = true
            scaleAnimate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
            circle.layer.addAnimation(scaleAnimate, forKey: "scaleSmallAnimation")
        })

        let opacityAnimation = CABasicAnimation(keyPath: "opacity")
        opacityAnimation.repeatCount = 1
        opacityAnimation.autoreverses = true
        opacityAnimation.fromValue = 1.0
        opacityAnimation.toValue = 0.40
        opacityAnimation.duration = 2.0

        circle.layer.addAnimation(opacityAnimation, forKey: "opacity")
        CATransaction.commit()
    }