1

I want to animate several properties of several subviews using Core Animations in Swift. I'm looking for a way to group the animations together and handle them as one, mainly to be able to synchronise them altogether and pause and resume all of them together, but also in order to avoid code duplication and make the code cleaner.

I tried to use other kind of animations, such as UIView.animate or UIViewPropertyAnimator, but neither of them fit my needs because they don't allow the customization needed for my case.

For now, I'm using CATransaction to ensure that the animation changes are committed to Core Animation at the same time:

CATransaction.begin()
CATransaction.setAnimationDuration(1.5)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(controlPoints: 0.85, 0, 0.15, 1))

let opacityAnimation = CABasicAnimation(keyPath: "opacity")
opacityAnimation.fromValue = 0
opacityAnimation.toValue = 1.0
opacityAnimation.repeatCount = .infinity
opacityAnimation.autoreverses = true
self.lable.layer.add(beforeOpacityAnimation, forKey: "opacityAninmation")

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = 1.5
scaleAnimation.toValue = 1.0
scaleAnimation.repeatCount = .infinity
scaleAnimation.autoreverses = true
self.imageView.layer.add(targetImageScaleAnimation, forKey: "scaleAnimator")

CATransaction.commit()

So far so good. But now I want to be able to pause and resume all animations together. From what I see here, in order to achieve this one would need to iterate over all animated layers (e.g. self.lable.layer, self.imageView.layer) to modify their speed, timeOffset and beginTime properties (this is kinda dirty and nonatomic).

Is there a way to group these animations or refer to them as one in order to pause and resume them altogether?

If not, should I at least put these iterations over layers inside a CATransaction as well?

MiZioLi5
  • 11
  • 3
  • 1
    It seems like you're looking for [`CAAnimationGroup`](https://developer.apple.com/documentation/quartzcore/caanimationgroup#). Have you tried it? – rob mayoff Jan 06 '23 at 22:13
  • @robmayoff It does group the animations, but according to the mentioned developer.apple answer, one has to pause and resume an animation via the animated layer, and I want to pause and resume multiple animations altogether, not layer-by-layer. – MiZioLi5 Jan 06 '23 at 22:19
  • The documentation I linked says “The grouped animations run in the time space specified by the `CAAnimationGroup` instance.” So I assume if you set the `speed` of the group to 0, it pauses all grouped animations. Is that not so? – rob mayoff Jan 06 '23 at 23:47
  • @robmayoff It could have worked if the entire group was added as an animation layer to the subviews; but since I'm animating different properties of different subviews, I cannot add the entire animation group to all layers (as that would animate both the opacity and the scale of both subviews). – MiZioLi5 Jan 07 '23 at 17:22

0 Answers0