【问题标题】:Present ViewController with custom animation使用自定义动画呈现 ViewController
【发布时间】:2013-04-28 23:19:43
【问题描述】:

我正在展示一个使用 foll 的视图控制器。代码:

VC_B * b = [[VC_B alloc] init...];
[self presentViewController:b animated:YES completion:nil];

我正在尝试为视图控制器的外观设置动画:传入的 VC 应该从当前显示的顶部覆盖 VC 向下滑动。以下this 解决方案使其可以解决一个问题:当前 VC 在传入的 VC 出现在屏幕上之前消失。有没有办法解决这个问题?或许有另一种解决方案来实现这种效果。

【问题讨论】:

    标签: ios objective-c animation uiviewcontroller


    【解决方案1】:

    试试这个代码:

    CreateNewViewController *newViewController = [[CreateNewViewController alloc]initWithNibName:@"CreateNewViewController" bundle:nil];
    
        CATransition *transition = [CATransition animation];
        transition.duration = 0.05;
        transition.timingFunction = [CAMediaTimingFunction      
        functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type =kCATransitionMoveIn;
        transition.subtype =kCATransitionFromTop;
    
        transition.delegate   = self;
    
        [self presentModalViewController:newViewController animated:NO];
        [self.view insertSubview:newViewController.view atIndex:0];
    
        [self.view.layer addAnimation:transition forKey:nil];
    

    希望这会对您有所帮助。

    【讨论】:

      【解决方案2】:

      试试下面的代码。您只需要从要显示 NewViewController 的位置调用以下方法。您需要做的是,您只需要更改NewViewcOntroller的View Frame的OriginY即可。

      -(void)displayNewVC
       {
      
       YourNewViewController *newVC = [[YourNewViewController alloc] init];
      
      CGFloat originY = -450;//set originY as you want to display newViewController from the Top to bottom with animation
      //initially set originY out of the Frame of CurrentViewcontroller
      newVC.view.frame = CGRectMake(orginX, originY, newVC.view.frame.size.width, newVC.view.frame.size.height);
      
      /*Display View With Animation*/
       originY = 300;
      [UIView animateWithDuration:.3 animations:^{
           //set new originY as you want.
          newVC.view.frame = CGRectMake(orginX, originY, newVC.view.frame.size.width,  newVC.view.frame.size.height);
          
       } completion:NULL];
      
       [currentViewController.view addSubview:newVC.view];
      
       }
      

      【讨论】:

      • 你能解释一下animateWithDuration调用中的originY用法吗?
      • @Asahi 这将是您希望在 CurrentViewController 中显示您的 NewViewControler 的地方。我只是举了一个例子,试试看....
      • 原始答案中没有使用它。现在好了 - 将尝试。
      • 无论如何要使用 presentViewController 而不是 addSubview?
      • @Asahi Nops,举个例子,因为你在谈论自定义动画。好吧,这取决于你想使用。
      猜你喜欢
      • 1970-01-01
      • 2020-12-20
      • 2020-04-27
      • 2017-12-06
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多