用BOOL /完成块停止自动反向/无限重复UIViewanimation

我设置了以下UIView animateWithDuration:方法,目的是在程序的其他地方设置我的animationOn BOOL来取消无限循环的重复。 我的印象是,每当animation的一个循环结束时就会调用completion块,但看起来并不是这样。

completion块是否在重复animation中被调用? 如果不是,还有另一种方法可以阻止这种方法以外的animation?

 - (void) animateFirst: (UIButton *) button { button.transform = CGAffineTransformMakeScale(1.1, 1.1); [UIView animateWithDuration: 0.4 delay: 0.0 options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations: ^{ button.transform = CGAffineTransformIdentity; } completion: ^(BOOL finished){ if (!animationOn) { [UIView setAnimationRepeatCount: 0]; } }]; } 

完成块只会在animation中断时被调用。 例如,当应用程序在后台运行并再次返回到前台(通过多任务处理)时,它会被调用。 在这种情况下,animation停止。 发生这种情况时,应该重新启动animation。

要停止animation,您可以从视图的图层中删除它:

 [button.layer removeAllAnimations]; 

老,但另一种select。

您还可以设置另一个不在同一个视图上重复的animation,这样您也可以在当前状态下捕获它,并使用选项UIViewAnimationOptionBeginFromCurrentState将其返回。 你的完成块也被称为。

 -(void)someEventSoStop { button.transform = CGAffineTransformMakeScale(1.0, 1.0); [UIView animateWithDuration: 0.4 delay: 0.0 options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState animations: ^{ button.transform = CGAffineTransformIdentity; } completion: ^(BOOL finished){ }]; } 

我已经通过调用[button.layer removeAllAnimations]解决了这个问题。

根据View类引用的文档:如果您使用了任何类方法,比如animateWithDuration:delay:options:animations:completion:如果持续时间设置为负值或0,则不进行animation更改。 所以我做了这样的事情来阻止无限的animation:

 [UIView animateWithDuration:0.0 animations:^{ button.layer.affineTransform = CGAffineTransformIdentity; }]; 

我认为这比从build议的答案中删除层中的所有animation更好。 请注意,这适用于UIView类中的所有其他类animation方法。