等到动画块在segue之前完成

时间:2012-08-01 09:42:57

标签: xcode ios5 animation segue block

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

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

情景非常简单:

在segue模式调出新屏幕之前,三张卡必须以动画退出屏幕。

到目前为止,他已经执行了动画并且加载了新视图,但我无法解决的细节是"等到动画完成之后再进行新视图"

我正是这样做的:

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"之前,尝试将segue代码包装在动画中。 VC

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self performExitAnimationWithCompletionBlock:^(BOOL finished)
     {
         // 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;
         }         
     }];    
}

正如我之前所说,一切正常,但模态窗口在动画结束前出现。

知道我错过了什么?

1 个答案:

答案 0 :(得分:3)

不是在prepareForSeque方法中触发动画,而是在想要开始动画处理时尝试调用[self performExitAnimationWithCompletionBlock],然后使用[self performSegueWithIdentifier]方法手动触发seque。最终动画的完成块。

我会假设你的seque当前连接到故事板中的按钮触摸。如果是这种情况,您将不得不在故事板中断开它。也许你可以通过连接到故事板上的隐形按钮来保持活塞的活着,这样它就永远不会被自动触发。

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

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

获得所需功能的另一种(也许更好的?)解决方案是根本不使用seque,而是手动转换屏幕。在这种情况下,完全从故事板中删除seque。还要确保在故事板中为OptionsViewController提供标识符。对导致动画/转换的操作执行以下代码:

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