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!
Asked
Active
Viewed 3,445 times
4

jww
- 97,681
- 90
- 411
- 885

Dhruv Ramani
- 2,633
- 2
- 22
- 29
-
One option would be to put the animation in a function. Then in the completion handler of the animation create a timer to call the function again after a delay of 3 seconds. – Daniel Storm Mar 19 '15 at 17:11
-
Could you tell me about completion handlers in Core Animation – Dhruv Ramani Mar 19 '15 at 17:15
-
Here's one example :http://stackoverflow.com/questions/9049410/how-can-i-detect-the-completion-of-an-animation-triggered-by-catransaction . And another using blocks: http://objcolumnist.com/2010/09/19/core-animation-using-blocks/ . Both of these are in ObjC though... – Daniel Storm Mar 19 '15 at 17:21
3 Answers
4
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)
}
}
}

Roman Podymov
- 4,168
- 4
- 30
- 57

rakeshbs
- 24,392
- 7
- 73
- 63
3
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.

David Rönnqvist
- 56,267
- 18
- 167
- 205
2
You could also use a UIView keyframe animation (animateKeyframesWithDuration) where there is "dead time" built into the animation at the end, and then repeat that animation.

Duncan C
- 128,072
- 22
- 173
- 272
-
1
-
I am still working in Objective-C. The idea is that you use an outer call to animateKeyframesWithDuration, with a total duration. Then inside that you make one or more calls to `addKeyframeWithRelativeStartTime:relativeDuration:animations:`. With that method you specify a last keyframe that start before the end of the whole duration, and ENDS before the whole duration. – Duncan C Mar 21 '15 at 11:59