iPhone:如何从左到右制作presentModalViewController动画

发布于 2024-10-09 00:54:58 字数 104 浏览 3 评论 0原文

我正在展示带有动画的模型视图。默认情况下,它是从下到上的。如何让动画从左向右播放?我知道我可以使用导航控制器。但实际上呈现视图不需要导航栏,模态呈现视图也不需要导航栏。我仍然想要从左到右的过渡。

I am presenting a model view with animation. As a default it comes from bottom to top. How can I make the animation to go from left to right? I know I could use a navigation controller. But actually the presenting view does not need a navigation bar and also the modally presented view does not need a navigation bar. Still I want a transition from left to right.

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

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

发布评论

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

评论(6

无人问我粥可暖 2024-10-16 00:54:58

您可以使用以下代码在呈现视图控制器时从右到左制作动画

CATransition *transition = [CATransition animation];
            transition.duration = 0.4;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            transition.type = kCATransitionPush;
            transition.subtype = kCATransitionFromRight;
            [self.view.window.layer addAnimation:transition forKey:nil];
            [self presentViewController:localitiesView animated:NO completion:nil];

You can animate from right to left while presenting a view controller by using the following code

CATransition *transition = [CATransition animation];
            transition.duration = 0.4;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            transition.type = kCATransitionPush;
            transition.subtype = kCATransitionFromRight;
            [self.view.window.layer addAnimation:transition forKey:nil];
            [self presentViewController:localitiesView animated:NO completion:nil];
旧城烟雨 2024-10-16 00:54:58

只有四个UIModalTransitionStyle

UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl

就像你说的,导航控制器会这样推动。如果您不想使用它,则必须自己为视图设置动画。

There are only four UIModalTransitionStyles:

UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl

Like you said, a nav controller will push that way. If you don't want to use that, you'll have to animate the view yourself.

友谊不毕业 2024-10-16 00:54:58

我在实现 Matthew 的解决方案时遇到了问题,因为在动画期间呈现模态视图的视图将不可见(相反,呈现视图将替换为窗口背景,然后模态视图将在其上进行动画处理),这导致了不和谐的体验。相反,我将模态视图作为子视图添加到当前 UIViewController 的视图中,并对其进行动画处理。我请求了不同的动画,因此我尝试更改一些值来表示您正在描述的动画,但我尚未实际测试下面的代码。

    UIViewController *modalView = //init your UIViewController
    [modalView loadView];
    CGRect finalFrame = modalView.view.frame;
    [modalView.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [UIView animateWithDuration:1.0 animations:^{
                    [self.navigationController setNavigationBarHidden:YES];
                    [self.view addSubview:modalView.view];
                    [modalView.view setFrame:finalFrame];
                }];

希望这有帮助。

I ran into a problem implementing Matthew's solution because the view presenting my modal view would not be visible during the animation (instead the presenting view would be replaced with the Window background and the modal view would then animate over that) which led to a jarring experience. Instead I've added the modal view as a subview to the presenting UIViewController's view and animated it across. I had a different animation requested so I've tried to change some of the values to represent the animation you're describing but I have not actually tested the code below.

    UIViewController *modalView = //init your UIViewController
    [modalView loadView];
    CGRect finalFrame = modalView.view.frame;
    [modalView.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [UIView animateWithDuration:1.0 animations:^{
                    [self.navigationController setNavigationBarHidden:YES];
                    [self.view addSubview:modalView.view];
                    [modalView.view setFrame:finalFrame];
                }];

Hope this helps.

超可爱的懒熊 2024-10-16 00:54:58

UINavigationController 有一个 navigationBarHidden 属性 - 如果将其设置为 YES,您可以获得从左到右的过渡样式以及导航控制器的其他细节,而无需可见导航栏。

UINavigationController has a navigationBarHidden property—if you set that to YES, you can get the left-to-right transition style and the other niceties of a navigation controller without having a visible navigation bar.

眉目亦如画i 2024-10-16 00:54:58

只是想展示另一个选项 - 这个选项允许您使用 UIView 动画从上到下呈现视图。
请注意,您必须在隐藏状态下添加新视图 - 确保动画在视图完全加载时开始。

AddViewController *controller = [[AddViewController alloc] initWithNibName:@"AddViewController" bundle:[NSBundle mainBundle]];
    controller.blogs = self.blogs;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    navController.toolbarHidden = YES;
    navController.navigationBarHidden = YES;
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentModalViewController:navController animated:NO];

    navController.view.frame = CGRectMake(0, -480, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
    navController.view.hidden = NO;
    [UIView animateWithDuration:0.3
                     animations:^{
                         navController.view.frame = CGRectMake(0, 20, navController.view.frame.size.width, navController.view.frame.size.height);
                     }];

Just wanted to show another option - this one lets you present the view from top to bottom, using a UIView animation.
please note you have to add the new view in hidden state - ensuring the animation starts with the view fully loaded.

AddViewController *controller = [[AddViewController alloc] initWithNibName:@"AddViewController" bundle:[NSBundle mainBundle]];
    controller.blogs = self.blogs;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    navController.toolbarHidden = YES;
    navController.navigationBarHidden = YES;
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentModalViewController:navController animated:NO];

    navController.view.frame = CGRectMake(0, -480, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
    navController.view.hidden = NO;
    [UIView animateWithDuration:0.3
                     animations:^{
                         navController.view.frame = CGRectMake(0, 20, navController.view.frame.size.width, navController.view.frame.size.height);
                     }];
下雨或天晴 2024-10-16 00:54:58

你可以尝试这样的事情:

   UIViewController *fooViewController = [[[UIViewController alloc] init] autorelease];

        CGSize theSize = CGSizeMake(320, 460);
        fooViewController.view.frame = CGRectMake(0 - theSize.width, 0, theSize.width, theSize.height);
        [UIView beginAnimations:@"animationID" context:NULL];

        fooViewController.view.frame = CGRectMake(0, 0, 320, 460);

        [UIView commitAnimations];

You can try something like this:

   UIViewController *fooViewController = [[[UIViewController alloc] init] autorelease];

        CGSize theSize = CGSizeMake(320, 460);
        fooViewController.view.frame = CGRectMake(0 - theSize.width, 0, theSize.width, theSize.height);
        [UIView beginAnimations:@"animationID" context:NULL];

        fooViewController.view.frame = CGRectMake(0, 0, 320, 460);

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