Objective-C,动画块,动画与完成之间的延迟

3

我有几个动画块,它们都遵循这个基本格式,但延迟不同,以便它们一个接一个地触发:

[UIView animateWithDuration:.85 delay:3 options:opts animations:[animations objectAtIndex:ww] completion:[completions objectAtIndex:ww]];

选项仅仅是为了方便访问将UIViewAnimationOptionAutoreverse放在一个变量中。
我希望动画和完成之间有一定的延迟,这样图像就可以在返回原始位置之前保持新位置的状态。我考虑使用多个简单的animateWithDuration:animations:块,但是除非我遗漏了什么,否则我没有看到文档中如何使用延迟的方法。
@Paul.s 这是我使用您给我的代码:
void (^completion)(void) = ^{
[UIView animateWithDuration:.5
                  delay:5
                options:UIViewAnimationCurveLinear
             animations:[completions objectAtIndex:ww]
             completion:^(BOOL finished) {}];
};


// Call your existing animation with the new completion block
[UIView animateWithDuration:.5
                  delay:1
                options:UIViewAnimationCurveLinear
             animations:[animations objectAtIndex:ww]
             completion:^(BOOL finished) {
               completion();
             }];

供参考,这个动画非常简单,只是将一张图片从一个点移动到另一个点,然后再返回。它崩溃的位置在定义完成块的[UIView animateWithDuration:.5行,并且在第一部分动画运行后崩溃。

1个回答

6

如何将另一个动画传递给完成函数?

更新我已经更新了代码,使其成为我设置的示例中的确切工作代码。这是使用“空应用程序”模板设置的干净项目。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    CGRect startFrame = CGRectMake(0, 0, 100, 100);

    UIView *view = [[UIView alloc] initWithFrame:startFrame];
    view.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view];

    // Set up your completion animation in a block
    void (^completion)(void) = ^{
        [UIView animateWithDuration:0.5f
                              delay:0.5f
                            options:UIViewAnimationCurveLinear
                         animations:^{
                             view.frame = startFrame;
                         }
                         completion:nil];
      };


      // Call your existing animation with the new completion block
      [UIView animateWithDuration:4
                            delay:1
                          options:UIViewAnimationCurveLinear
                       animations:^{
                           view.frame = CGRectMake(200, 200, 10, 10);
                       }
                       completion:^(BOOL finished) {
                           completion();
                       }];



        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
}

当它尝试开始运行完成动画时,它会抛出EXC_BAD_ACCESS错误。我已经将数字更改为所需的数字,但没有解决问题,不确定为什么会发生这种情况,因为代码看起来很好。 - lunadiviner
你能展示一下你使用的代码以及 EXC_BAD_ACCESS 发生的位置吗?除了两个调用你的 animations 之外,我发布的代码是来自一个正常工作的示例。 - Paul.s

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接