iOS - 停止重复的UIAnimation?

时间:2012-03-17 23:43:10

标签: ios uianimation

我有一个需要重复的动画,直到我决定停止它。

单击按钮后如何停止动画?

[UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{

        CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(5));
        self.transform = transform;

    }  completion:^(BOOL finished){

    }];

2 个答案:

答案 0 :(得分:12)

您必须添加另一个选项UIViewAnimationOptionAllowUserInteraction ...

你应该试试这个:

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^
{
    view.frame = CGRectMake(0, 100, 200, 200);
} completion:^(BOOL finished)
{
    if(! finished) return;
}];

要停止动画,请使用:

[view.layer removeAllAnimations];

答案 1 :(得分:2)

U可以改为使用CABasicAnimation。

    CABasicAnimation *appDeleteShakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
appDeleteShakeAnimation.autoreverses = YES;
appDeleteShakeAnimation.repeatDuration = HUGE_VALF;
appDeleteShakeAnimation.duration = 0.2;
appDeleteShakeAnimation.fromValue = [NSNumber numberWithFloat:-degreeToRadian(5)];
appDeleteShakeAnimation.toValue=[NSNumber numberWithFloat:degreeToRadian(5)];
[self.layer addAnimation:appDeleteShakeAnimation forKey:@"appDeleteShakeAnimation"];

然后,当你想要停止它时,你可以打电话

[self.layer removeAnimationForKey:@"appDeleteShakeAnimation"];