0

I have two animations that I want to chain into group.

They look like this :

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

Is there a way I can set separate duration to first animation and separate to second ? For example, I want 3 seconds for opacityAnimation and 1 second for scaleAnimation

How can I do this ?

Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
Alexey K
  • 6,537
  • 18
  • 60
  • 118

1 Answers1

1

try with this code and I didn't tested this code.

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()
    }
Antony Raphel
  • 2,036
  • 2
  • 25
  • 45