首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止特定的UIViewAnimation块- iOS

停止特定的UIViewAnimation块- iOS
EN

Stack Overflow用户
提问于 2015-07-08 06:23:31
回答 3查看 362关注 0票数 2

我有一个iOS应用程序,它运行几个不同的UIViewAnimation块来在屏幕上动画几个不同的对象。这一切都是可行的,但是我如何在不停止其他动画块的情况下停止其中一个动画块呢?

我尝试使用以下方法:

代码语言:javascript
复制
[button.layer removeAllAnimations];

但它什么也没做,动画还在继续。

然后,我尝试使用一个简单的BOOL值,并在将return设置为"NO“后,将动画从方法中获取到BOOL,但这也不起作用。

下面是我试图停止的动画:

代码语言:javascript
复制
-(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;
            }
        }];
    }];
}

谢谢丹。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-08 07:35:55

如果使用的是UIKit动画,则无法访问正在运行的动画属性。因此,我建议使用核心动画,如果你想修改动画在运行时。

移除下面视图中的alpha太简单了。

代码语言:javascript
复制
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"MyAnimation"]; 

在将动画添加到图层之后,动画将启动,您可以使用委托方法来了解animationDidFinish:方法

代码语言:javascript
复制
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    NSLog(@"Animation interrupted: %@", (!flag)?@"Yes" : @"No");
}

此外,你也可以达到任何时候,你想从使用;

代码语言:javascript
复制
[[moviepic layer] animationForKey:@"MyAnimation"];

当然,您需要将CoreAnimation框架添加到项目中。

希望能帮上忙。

票数 2
EN

Stack Overflow用户

发布于 2015-07-08 06:42:43

我认为简单的方法是从视图层中删除所有动画,因为默认情况下所有动画都添加到视图的层中。

代码语言:javascript
复制
[yourRequiredView.layer removeAllAnimations]; 
票数 1
EN

Stack Overflow用户

发布于 2015-07-08 06:47:04

我的理解是,从相关层删除所有动画应该停止所有动画。[((UIButton *)_buttons[num]).layer removeAllAnimations];怎么样?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31284965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档