等到动画块在segue之前完成 [英] Wait until animation block finishes before segue

查看:119
本文介绍了等到动画块在segue之前完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing some simple animations to my cards app.

到目前为止,一切都运行良好,但我还有一个细节需要修复才能说出来已完成。

Everything is working great so far but I have just one more detail to fix before I can say it is done.

情景非常简单:

必须退出三张卡片在segue模式之前带有动画的屏幕会显示新屏幕。

Three cards must exit the screen with an animation before segue modally brings up the new screen.

到目前为止,动画被执行并且新视图已加载,但细节我还没有能够解决的问题是等到动画结束后才能进入新视图。

Up until now he animation gets executed and the new view is loaded, but the detail I haven't been able to work out is the "wait until the animation finishes before bringing the new view".

这就是我这样做的方式:

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

1) Set exit animation with this method

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

2) Try to wrap the segue code within the animation before presenting the "AddOptions" 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;
         }         
     }];    
}

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

As I said before, everything works but the modal windows comes up before the animation finishes.

我不知道我缺少什么?

推荐答案

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

Instead of triggering the animation in the prepareForSeque method, you should try calling [self performExitAnimationWithCompletionBlock] when you want to start the animation process, and then trigger the seque manually using the [self performSegueWithIdentifier] method in the completion block of the final animation.

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

I'll assume that your seque it currently connected to a button touch in your storyboard. If that's the case you'll have to disconnect it in the storyboard. Perhaps you can keep the seque alive by connecting to an invisible button on your storyboard so that it can never be automatically fired.

相反,写下按钮代码如下: / p>

Instead, write the button code like so:

-(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提供标识符。对导致动画/转换的操作执行以下代码:

An alternative (and perhaps better?) solution to get the functionality you want is not to use a seque at all, but instead manually transition the screen. In this case, delete the seque entirely from the storyboard. Also ensure you provide an identifier for OptionsViewController in the storyboard. Execute the following code on the action that causes the animation/transition:

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

这篇关于等到动画块在segue之前完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆