Xcode 等待动画块完成,然后继续

Xcode 等待动画块完成,然后继续,xcode,ios5,animation,segue,block,Xcode,Ios5,Animation,Segue,Block,我正在为我的卡片应用程序实现一些简单的动画 到目前为止,一切都很顺利,但在我可以说一切都完成之前,我还有一个细节需要解决 场景非常简单: 在segue modally打开新屏幕之前,必须有三张卡以动画形式退出屏幕 到目前为止,动画被执行,新的视图被加载,但我还没有弄清楚的细节是“等到动画完成后再开始新的视图” 我就是这样做的: 1) 使用此方法设置退出动画 - (void)performExitAnimationWithCompletionBlock:(void (^)(BOOL))block

我正在为我的卡片应用程序实现一些简单的动画

到目前为止,一切都很顺利,但在我可以说一切都完成之前,我还有一个细节需要解决

场景非常简单:

在segue modally打开新屏幕之前,必须有三张卡以动画形式退出屏幕

到目前为止,动画被执行,新的视图被加载,但我还没有弄清楚的细节是“等到动画完成后再开始新的视图”

我就是这样做的:

1) 使用此方法设置退出动画

- (void)performExitAnimationWithCompletionBlock:(void (^)(BOOL))block
{    
    [UIView animateWithDuration:0.1f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         self.optionOneFront.center = self.optionOneBack.center = self.optionTwoFront.center;
         self.optionOneFront.transform = self.optionOneBack.transform = self.optionTwoFront.transform;

         self.optionThreeFront.center = self.optionThreeBack.center = self.optionTwoFront.center;
         self.optionThreeFront.transform = self.optionThreeBack.transform = self.optionTwoFront.transform;
     }
                     completion:^(BOOL finished)
     {
         CGPoint point = CGPointMake(self.optionTwoFront.center.x, self.view.frame.size.height * -2.0f);

         [UIView animateWithDuration:1.0f
                               delay:0.0f
                             options:UIViewAnimationOptionCurveEaseOut
                          animations:^
          {
              self.optionOneFront.center   = point;
              self.optionOneBack.center    = point;
              self.optionTwoFront.center   = point;
              self.optionTwoBack.center    = point;
              self.optionThreeFront.center = point;
              self.optionThreeBack.center  = point;
          }
                          completion:block];
     }];
}
2) 在展示“AddOptions”VC之前,尝试在动画中包装segue代码

正如我之前所说的,除了模式窗口在动画完成之前出现外,其他一切都正常


你知道我遗漏了什么吗?

当你想启动动画过程时,你应该尝试调用
[self-performExitAnimationWithCompletionBlock]
,而不是在
prepareForSeque
方法中触发动画,然后使用
[self-performSegueWithIdentifier]手动触发序列
最终动画完成块中的方法

我假设您的序列当前已连接到故事板中的按钮触摸。如果是这种情况,你必须在情节提要中断开它。也许你可以通过连接到你的故事板上的一个不可见按钮来保持序列的活性,这样它就永远不会被自动触发

相反,按如下方式编写按钮代码:

-(void)btnStartSeque:(id)sender{

      //trigger animations

      [self performExitAnimationWithCompletionBlock:^(BOOL finished){
         [self performSegueWithIdentifer:@"AddOptions" sender:self];
     }];    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   // Executes the following "if" statement if the user wants to add new options
    if ([segue.identifier isEqualToString:@"AddOptions"])
    {
      UINavigationController *navigationController = segue.destinationViewController;
      OptionsViewController *controller = (OptionsViewController *)navigationController.topViewController;
      controller.delegate = self;
    }               
}
获得所需功能的另一种解决方案(也许更好)是根本不使用序列,而是手动转换屏幕。在这种情况下,将序列从情节提要中完全删除。还要确保在情节提要中为选项ViewController提供标识符。对导致动画/过渡的操作执行以下代码:

-(void)btnStartModalTransition:(id)sender{

      //trigger animations

      [self performExitAnimationWithCompletionBlock:^(BOOL finished){

          //load the storyboard (sub in the name of your storyboard)
          UIStoryBoard* storyBoard = [UIStoryBoard storyBoardWithName:@"MyStoryBoard" bundle:nil]
          //load optionsviewcontroller from storyboard
          OptionsViewController *controller = (OptionsViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"OptionsViewControllerIdentifier"];
          UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];

         controller.delegate = self;
         [self presentModalViewController:navigationController animated:YES];
     }];    
}

你好,谢谢你的回复。您的建议改进了实施,但主要问题仍然存在。当序列进入时,performExitAnimationWithCompletionBlock未完成。另外,我猜既然我在button方法中调用了“AddOptions”,我就可以从prepareForSegue中删除它,对吗?“如果([segue.identifier isEqualToString:@“AddOptions”]”)我想已经不需要了。@JuanGonzález:是的,我没有注意到序列必须在情节提要中进行调整才能不自动触发。我还提供了一个完全不使用seques的替代解决方案,这可能会更好。请注意,我还没有实际测试过这些……没关系。你的回答给了我解决问题的方向。谢谢仅供参考,您仍然可以使用segue,无需将其置于不可见的按钮或任何东西上。只需让segue从一个屏幕转到另一个屏幕(而不是任何单个元素)。
-(void)btnStartModalTransition:(id)sender{

      //trigger animations

      [self performExitAnimationWithCompletionBlock:^(BOOL finished){

          //load the storyboard (sub in the name of your storyboard)
          UIStoryBoard* storyBoard = [UIStoryBoard storyBoardWithName:@"MyStoryBoard" bundle:nil]
          //load optionsviewcontroller from storyboard
          OptionsViewController *controller = (OptionsViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"OptionsViewControllerIdentifier"];
          UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];

         controller.delegate = self;
         [self presentModalViewController:navigationController animated:YES];
     }];    
}