按下后退按钮时的自定义动画 - iOS

时间:2015-04-04 04:04:20

标签: ios objective-c uinavigationcontroller pushviewcontroller

当用户按下uiviewcontroller导航栏的默认后退按钮(而不是自定义按钮)时,我正在尝试生成自定义效果。在尝试弹出视图并让视图控制器消失时,我试图将uiview向下移动到某个Y位置。

我尝试了两种方法:

方法1

    - (void)viewWillDisappear:(BOOL)animated
{
  [super viewWillDisappear:animated];

  [UIView animateWithDuration:0.2 animations:^{
    self.cvCell.frame = self.selectedCellFrame;
  } completion:^(BOOL finished) {
  [UIView animateWithDuration:0.75
                     animations:^{

                         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                         [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.navigationController.view cache:NO];
                     } completion:^(BOOL finished) {

                         [self.navigationController popViewControllerAnimated:NO];
                     }];
    }];


}

方法2

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{


if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {

    [UIView animateWithDuration:0.2 animations:^{
       self.cvCell.frame = self.selectedCellFrame;
    } completion:^(BOOL finished) {
       CATransition *transition = [CATransition animation];
       [transition setDuration:0.75];
       [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
       [transition setType:kCATransitionReveal];
       [transition setSubtype:kCATransitionFromLeft];
       [transition setDelegate:self];
       [self.navigationController.view.layer addAnimation:transition forKey:nil];
     }];
  }
}

在这两种情况下,视图在UIView上的动画开始之前消失。 有关实现这一目标的任何建议吗?

1 个答案:

答案 0 :(得分:0)

如果您在iOS应用中使用Storyboard,则可以尝试实现cusom segue动画。您可以覆盖方法prepareForSegue:sender:以检查事务是否应该发生以及方法perform:以在屏幕上显示新的视图控制器。然后,您可以继承UIStoryboardSegue并在其中启用自定义动画 iOS Docs