iPhone: How to do a presentModalViewController ani

2019-03-13 14:24发布

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.

6条回答
\"骚年 ilove
2楼-- · 2019-03-13 14:32

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.

查看更多
太酷不给撩
3楼-- · 2019-03-13 14:35

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];
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-03-13 14:36

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);
                     }];
查看更多
倾城 Initia
5楼-- · 2019-03-13 14:44

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-03-13 14:45

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];
查看更多
霸刀☆藐视天下
7楼-- · 2019-03-13 14:52

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.

查看更多
登录 后发表回答