我有一个iOS应用程序,它运行几个不同的UIViewAnimation块来在屏幕上动画几个不同的对象。这一切都是可行的,但是我如何在不停止其他动画块的情况下停止其中一个动画块呢?
我尝试使用以下方法:
[button.layer removeAllAnimations];但它什么也没做,动画还在继续。
然后,我尝试使用一个简单的BOOL值,并在将return设置为"NO“后,将动画从方法中获取到BOOL,但这也不起作用。
下面是我试图停止的动画:
-(void)undo_animation:(int)num {
// Fade out/in the number button label
// animation which lets the user know
// they can undo this particular action.
[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
// Mostly fade out the number button label.
((UIButton *)_buttons[num]).alpha = 0.2;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
// Fade in the number button label.
((UIButton *)_buttons[num]).alpha = 1.0;
} completion:^(BOOL finished) {
// Stop the animation if the BOOL
// is set to 'NO' animations.
if (anim_state_button == NO) {
return;
}
}];
}];
}谢谢丹。
发布于 2015-07-08 07:35:55
如果使用的是UIKit动画,则无法访问正在运行的动画属性。因此,我建议使用核心动画,如果你想修改动画在运行时。
移除下面视图中的alpha太简单了。
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"MyAnimation"]; 在将动画添加到图层之后,动画将启动,您可以使用委托方法来了解animationDidFinish:方法
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSLog(@"Animation interrupted: %@", (!flag)?@"Yes" : @"No");
}此外,你也可以达到任何时候,你想从使用;
[[moviepic layer] animationForKey:@"MyAnimation"];当然,您需要将CoreAnimation框架添加到项目中。
希望能帮上忙。
发布于 2015-07-08 06:42:43
我认为简单的方法是从视图层中删除所有动画,因为默认情况下所有动画都添加到视图的层中。
[yourRequiredView.layer removeAllAnimations]; 发布于 2015-07-08 06:47:04
我的理解是,从相关层删除所有动画应该停止所有动画。[((UIButton *)_buttons[num]).layer removeAllAnimations];怎么样?
https://stackoverflow.com/questions/31284965
复制相似问题