Search code examples
iosanimationcore-animationcalayer

Core Animation: Do an animation only on a specified-portion of a layer (iOS)


I'm pretty much desperate here looking for a way to add an animation to a CALayer but make it apply only to a part of the layer. Or if it's even possible.

I'm trying to animate a part of the text (currently using UILabel) but keep the rest as-is.

Here's a good example:

A counter, when you add/remove a count only one digit changes.

Thanks

Edit 1:

I'm trying to edit the position of a /part/ of the text.


Solution

  • What you describe is NOT possible. Core animation applies to an entire layer. UIView animation applies to an entire view.

    If you want to animate part of a layer/view, you't going to need to cut it into pieces and animate just the part you want to change.

    An exception to this is that you can animate the path of a CAShapeLayer. As long as the path keeps the same structure (number/type of control points) then changes to the path get animated.

    If you had a shape layer where the text was defined using a path, and you could devise a way to transform the path while keeping the same number and type of control points, you could make part of the shape change.

    It's also possible to animate changes to a layer's mask. With that, you can reveal or hide part of a layer, which could give you an effect like animating part of a layer. You could make the middle portion of a layer fade away to transparent, for example.