Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay after each animation iteration in Swift

I have a Core Animation whose .repeatCount is set to Float.infinity. After each iteration of the Animation, ie. after each repetition, I want to have a delay of 3 seconds. How can I achieve this? Thanks!

like image 201
Dhruv Ramani Avatar asked Nov 05 '25 12:11

Dhruv Ramani


2 Answers

One way to accomplish this effect using Core Animation is to configure everything except the repeat count on the original animation object and then wrap it in an animation group with a longer duration and repeat that animation group instead.

let originalAnimation = /* create and configure original animation ... */
originalAnimation.duration = shortDuration

let group = CAAnimationGroup()
group.animations = [originalAnimation]
group.duration = shortDuration + delayAtTheEnd
group.repeatCount = .infinity

theLayer.add(group, forKey: "repeating animation with delay between iterations")

Note that depending on what the original animation is doing, you may need to configure a fill mode to achieve the right look.

like image 70
David Rönnqvist Avatar answered Nov 07 '25 02:11

David Rönnqvist


You can use a function like the following to do what you need.

func animateInfinitelyWithDelay(delay: TimeInterval, duration: TimeInterval) {

    UIView.animate(
        withDuration: duration, 
        delay: delay, 
        options: UIView.AnimationOptions.curveEaseIn,
        animations: { () -> Void in

        // Your animation Code                        
    }) { (finished) -> Void in
        if finished {
            self.animateInfinitelyWithDelay(delay: delay, duration: duration)
        }
    }

}
like image 41
rakeshbs Avatar answered Nov 07 '25 02:11

rakeshbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!