你如何从屏幕顶部而不是底部呈现UIViewController?

时间:2011-08-31 23:36:22

标签: iphone uiview uiviewcontroller transition uimodaltransitionstyle

我正在使用UIViewController并且我使用presentModalViewController:controllerr animated:YES来呈现它但我想它是否会从屏幕顶部向下滑动而不是从底部向下滑动,任何方式都可以这样做?

5 个答案:

答案 0 :(得分:7)

我创建了一个从所有4个方向推送ViewControllers的功能:

- (void) slideLayerInDirection:(NSString *)direction andPush:(UIViewController *)dstVC {
  CATransition* transition = [CATransition animation];
  transition.duration = 0.5;
  transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  transition.type = kCATransitionPush;
  transition.subtype = direction;
  [self.view.layer addAnimation:transition forKey:kCATransition];
  [self pushViewController:dstVC animated:NO];
}

头文件包含以下内容:

#import <QuartzCore/QuartzCore.h>

#define direction_left kCATransitionFromLeft
#define direction_top kCATransitionFromBottom
#define direction_right kCATransitionFromRight
#define direction_bottom kCATransitionFromTop

答案 1 :(得分:3)

public extension UINavigationController {

    func pushViewControllerFromTop(viewController vc: UIViewController) {
        vc.view.alpha = 0
        self.present(vc, animated: false) { () -> Void in
            vc.view.frame = CGRect(x: 0, y: -vc.view.frame.height, width: vc.view.frame.width, height: vc.view.frame.height)
            vc.view.alpha = 1
            UIView.animate(withDuration: 1, 
                           animations: { () -> Void in
                               vc.view.frame = CGRect(x: 0, y: 0, width: vc.view.frame.width, height: vc.view.frame.height)
                           }, 
                           completion: nil)
        }
    }

    func dismissViewControllerToTop() {
        if let vc = self.presentedViewController {
            UIView.animate(withDuration: 1, 
                           animations: { () -> Void in
                               vc.view.frame = CGRect(x: 0, y: -vc.view.frame.height, width: vc.view.frame.width, height: vc.view.frame.height)
                           }, 
                           completion: { complete -> Void in
                               if complete {
                                   self.dismiss(animated: false, completion: nil)
                               }
                           })
        }
    }
}

答案 2 :(得分:2)

我使用以下代码从顶部为viewController设置动画。

[self.mainViewController.view addSubview:modalViewController.view];
modalViewController.view.frame = CGRectMake(modalViewController.view.frame.origin.x,
                                            -modalViewController.view.frame.size.height,
                                            modalViewController.view.frame.size.width,
                                            modalViewController.view.frame.size.height);

[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){

    modalViewController.view.frame = CGRectMake(modalViewController.view.frame.origin.x,
                                                0,
                                                modalViewController.view.frame.size.width,
                                                modalViewController.view.frame.size.height);

} completion:^(BOOL finished){

    [modalViewController.view removeFromSuperview];
    [self.mainViewController presentViewController:modalViewController animated:NO completion:nil];

}];

它将UIView的{​​{1}}添加到modalViewController,为其添加动画效果,然后从mainViewController和{{1}中移除UIView没有动画。

答案 3 :(得分:1)

您所描述的不是内置的过渡风格;请参阅here以获取Apple提供的列表。如果您绝对需要您的视图控制器以这种方式显示,则您必须自己制作动画。

答案 4 :(得分:1)

您必须#import QuartzCore框架并添加过渡动画,然后更改:

animated:YES 至: animated:NO