0

I have a sub-layer keyframe animation defined to animate an image along a BSpline, grouped together with a rotate animation. With the sub-layer.speed set to 0, I can drag the image back and forth along the curve by adjusting the animationsGroup.timeOffset value based on the distance dragged. What I want to do is, after a certain threshold (say %15) is to set the animation speed to 1 so the animation completes by itself, but it's not that simple. The animation either completes immediately and resets everything back to the start position, or the animiation reaches the end of the path, loops round to zero, and continues animating until it's back to the point where the animation kicks in.

What I want is:

Tstart -> drag -> T0.15 -> animation -> Tend

But what I'm getting is

Tstart -> drag -> T0.15 -> animation -> Tend -> Tstart -> T0.15

I've already looked into use of timeOffset and time-warps, and fiddled with the parameters but to no avail.

1 Answers1

0

You can that outcome by wrapping your animation group in a new animation group whose duration is 0.85 times the animation groups duration. Here is a bit of code that does just that:

- (void) setTimeOffset:(double)value
{
    if (value < 0.15) {

        // This is what you are already doing.
        _animGroup.timeOffset = value;
        [_someSublayer addAnimation:_animGroup forKey:@"animGroup"];

    } else if (_animGroup.speed == 0.0) {

        // You must set the final position before animating otherwise the 
        // layer will come back to it's original position.

        _someSublayer.position = _theEndPointOfYourPath;

        // Now set the speed of the animation group to 1 so that it animates.
        // Be sure to leave the timeOffset alone so that is starts where you want it.

        _animGroup.speed = 1.0;

        // Create a new animation group around it whose duration is the remaining time
        // of the animation group and add that to the layer instead. It will prevent
        // animGroup from wrapping.

        CAAnimationGroup *outerGroup = [[CAAnimationGroup alloc] init];
        outerGroup.animations = @[_animGroup];
        outerGroup.duration = _animGroup.duration - _animGroup.timeOffset;

        [_someSublayer addAnimation:outerGroup forKey:@"animGroup"];
    }    
}
aLevelOfIndirection
  • 3,522
  • 14
  • 18