简体   繁体   English

从侧面显示UIViewController

[英]Present UIViewController from Side

I'm trying to present a UIViewController from the side. 我正在尝试从侧面展示UIViewController。 The only thing that Present Modal can do is Verticle Slide in. Present Modal唯一可以做的就是垂直滑入。

Any body have any ideas about this? 有谁对此有任何想法?

Let me know' thanks 让我知道,谢谢

I did this and it worked, though I don't know if there is a more official way to do it (UIVIewAimationTransition doesn't have a slide option): 我做到了,并且奏效了,尽管我不知道是否有更正式的方法(UIVIewAimationTransition没有幻灯片选项):

    UIView *currentView = self.view;
UIView *theWindow = [currentView superview];

[currentView removeFromSuperview];

CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];

To be anal about the MVC , its really the View your moving and not the controller. 关于MVC ,实际上是View(而不是控制器)。

I would recommended the way of doing this would be to use a navigation controller, it's animation is a horizontal slide by default and you get a navigation stack for free- this means that you have back buttons auto generated and, title and button properties that you can set, although these can be hidden easily is desired. 我建议这样做的方法是使用导航控制器,默认情况下它的动画是水平幻灯片,并且您可以免费获得导航堆栈-这意味着您可以自动生成后退按钮,并且可以使用标题和按钮属性可以设置,尽管可以轻松隐藏它们。

Using a navigation controller: 使用导航控制器:

create a UINavigationsController property in your parent controller, syth, alloc and init it appropriately. 在父控制器syth,alloc中创建一个UINavigationsController属性,并对其进行适当的初始化。 Then you can load the controllers in in any order you want and it looks after the navigation. 然后,您可以按所需的任何顺序加载控制器,并在导航后进行管理。

Here is a good tutorial on NavControllers (usually they are used with UITableViewControllers but don't have to be) 是有关NavControllers的很好的教程(通常它们与UITableViewControllers一起使用,但不一定要使用)

Peng's example is an animation and I should note that it removes the parent view (and depending on your design might not just remove the 'currentView' - as your/a top view might be a subView) so if your navigating back you would have to handle this yourself. Peng的示例是一个动画,我应该注意,它删除了父视图(并且视您的设计而定,可能不只是删除“ currentView”,因为您/顶视图可能是子视图),因此,如果您向后导航,则必须自己处理。

I just gave this answer to another SO question that was very similar, but here it is again, in case folks looking end-up here, instead. 我只是将这个答案提供给另一个非常类似的SO问题,但还是在这里,以防万一人们最终来这里。

All of the ViewControllers in my app inherit from MyViewController which, in addition to some other default behaviours, has this property: 我应用程序中的所有ViewControllers都继承自MyViewController ,除了一些其他默认行为之外, MyViewController还具有以下属性:

@property(readwrite, nonatomic) BOOL slideFromSide;

And this code: 这段代码:

- (void) presentViewController: (UIViewController*) vc animated: (BOOL) flag completion: (void (^)(void)) completion
{
    BOOL slideIn = NO;
    if ([vc isKindOfClass: [MyViewController class]])
    {
        MyViewController *svc = (MyViewController*) vc;
        slideIn = svc.slideFromSide;
    }

    if (slideIn)
    {
        [self addChildViewController: vc];
        [self.view addSubview: vc.view];

        CGRect frame = vc.view.frame;
        frame.origin.x = frame.size.width;
        vc.view.frame = frame;

        frame.origin.x = 0;
        [UIView animateWithDuration: 0.33
                         animations: ^{
                             vc.view.frame = frame;
                         }
         ];
    }
    else
    {
        [super presentViewController: vc animated: flag completion: completion];
    }
}


- (IBAction) backAction: (id) sender
{
    if (_slideFromSide)
    {
        CGRect frame = self.view.frame;
        frame.origin.x = frame.size.width;
        [UIView animateWithDuration: 0.33
                         animations: ^{
                             self.view.frame = frame;
                         }
                         completion:^(BOOL finished) {
                             [self removeFromParentViewController];
                         }
         ];
    }
    else
    {
        [self dismissViewControllerAnimated: YES completion: nil];
    }
}

Then, in the ViewControllers where I want to slide-in, I have: 然后,在要滑入的ViewControllers中,我有:

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
    if (self)
    {
        self.slideFromSide = YES;
        // Whatever else you want to do
    }

    return self;
}

Calling a new ViewController is just like normal: 调用新的ViewController就像正常情况一样:

WhateverViewController *vc = [WhateverViewController alloc] initWithNibName: nil bundle: nil];
[self presentViewController: vc animated: YES completion: nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM