4

I have an animation that needs to be repeated until I decided to stop it.

How can I stop in animation after a button click?

[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

もう1つのオプションを追加する必要があります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 に答える