ios - 停止特定的 UIView 动画 block - iOS

标签 ios objective-c animation uiviewanimation animatewithduration

我有一个 iOS 应用程序,它运行一些不同的 UIViewAnimation block 来为屏幕上的一些不同对象设置动画。这一切都有效,但是我怎样才能停止其中一个动画 block 而不停止其余的动画 block 呢?

我尝试过使用以下方法:

[button.layer removeAllAnimations];

但它没有做任何事情,动画只是继续。

然后,我尝试使用一个简单的 BOOL 值,并在 BOOL 设置为“NO”后让动画从该方法返回 “,但这也行不通。

这是我试图停止的动画:

-(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;
            }
        }];
    }];
}

谢谢,丹。

最佳答案

如果您使用 UIKit 动画,则无法访问正在运行的动画属性。所以如果你想在运行时修改动画,我建议使用Core Animation。

像下面这样删除 View 的 Alpha 太简单了。

CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"MyAnimation"]; 

将动画添加到图层后,动画将开始,然后您可以使用委托(delegate)方法来了解animationDidFinish:方法

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    NSLog(@"Animation interrupted: %@", (!flag)?@"Yes" : @"No");
}

您还可以随时使用;

[[moviepic layer] animationForKey:@"MyAnimation"];

当然,您需要将 CoreAnimation Framework 添加到您的项目中。

希望有帮助。

关于ios - 停止特定的 UIView 动画 block - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31284965/

相关文章:

android - 使事物在屏幕上移动的动画 (Android)

ruby-on-rails - Rails 和 iOS 企业版

iphone - 自定义 UITableView

iphone - UIView动画问题(将扩展动画与收缩动画匹配)

ios - UIView Swift 倾斜动画

iOS 持久缓存目录

ios - 如何在水平滚动时将 Collection View 中的内容居中于显示的环形 View 的正中心(iOS)?

javascript - 在哪里放置用于创建 Cordova 插件的 Javascript?

ios - TableViewController 在状态栏下滚动

objective-c - 无需解锁对话框即可读取权限