Search code examples
iosanimationcore-animationrotation

how can I interrupt/stop animation when I rotate?


For example, I use the animation in landscape status, duration is 5.0s, from status A to B; in the middle of the 5.0s, I may rotate the iPad from landscape to portrait. I want the animation stopped and make the UI status to C after I rotated.

I'm not sure my question is clear. How can I do that?

my animation code:

- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration x:(NSNumber*)dx y:(NSNumber*)dy
{

    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    float fx = [dx floatValue];
    float fy = [dy floatValue];

    CGAffineTransform transform = CGAffineTransformMakeTranslation(fx, fy);
    //CGAffineTransform transform = CGAffineTransformMakeRotation(0.4);
    //CGAffineTransform transform = CGAffineTransformMakeScale(2.0, 2.0);

    image.transform = transform;

    // Commit the changes
    [UIView commitAnimations];    
}

Solution

    1. Starting and Stopping Explicit Animations has a section on starting and stopping core animations
    2. willRotateToInterfaceOrientation:duration notifies you when the rotation begins.

    So all have to do is 1) from 2)