4

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

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

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

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

    }  completion:^(BOOL finished){

    }];
4

2 回答 2

13

您必须添加另一个选项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];
于 2013-03-04T12:41:26.750 回答
2

你可以使用 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"];
于 2013-02-06T10:25:02.500 回答