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

发布于 2024-12-02 18:35:45 字数 124 浏览 6 评论 0原文

我正在使用 UIViewController 并使用 PresentModalViewController:controllerr 动画:YES 来呈现它,但我想如果它从屏幕顶部向下滑动而不是从底部向上滑动,有什么方法可以做到这一点?

I am using a UIViewController and I use presentModalViewController:controllerr animated:YES to present it but I would like if it would slide down from the top of the screen instead of up from the bottom, any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

世界等同你 2024-12-09 18:35:45

我创建了一个用于从所有 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

I created a function for pushing the ViewControllers from all 4 directions:

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

The header file contains the following:

#import <QuartzCore/QuartzCore.h>

#define direction_left kCATransitionFromLeft
#define direction_top kCATransitionFromBottom
#define direction_right kCATransitionFromRight
#define direction_bottom kCATransitionFromTop
甜扑 2024-12-09 18:35:45
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)
                               }
                           })
        }
    }
}
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)
                               }
                           })
        }
    }
}
北方的韩爷 2024-12-09 18:35:45

我使用以下代码从顶部对视图控制器进行动画处理。

[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];

}];

它将 modalViewControllerUIView 添加到 mainViewController,为其添加动画,然后从 mainViewController 中删除 UIView。 code>mainViewController 和 presentsViewController: 没有动画。

I use the following code to animate a viewController in from the top.

[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];

}];

It adds the UIView of the modalViewController to the mainViewController, animates it in, then removes the UIView from the mainViewController and presentsViewController: without animation.

秉烛思 2024-12-09 18:35:45

您所描述的不是内置的过渡样式;而是内置的过渡样式。请参阅此处查看 Apple 提供的列表。如果您绝对需要视图控制器以这种方式显示,则必须自己为其设置动画。

What you're describing isn't a built-in transition style; see here for a list of those that Apple provides. If you absolutely need your view controller to appear this way, you'll have to animate it yourself.

绅士风度i 2024-12-09 18:35:45

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

animated:YES
到:
动画:否

You have to #import the QuartzCore framework and add the transition animation, then change:

animated:YES
to:
animated:NO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文