在两个UIViewControllers的视图之间滑动 [英] Swipe between two UIViewControllers' views

查看:76
本文介绍了在两个UIViewControllers的视图之间滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我已经问了几次这个问题,但是我很缺乏经验,并且认为我还没有得到正确答案。

I know I have asked this question a couple of times, but I'm very inexperienced and don't think I've been given the right answer yet.

我现在有两个视图控制器,每个控制器都有一个.h文件,一个.m文件和一个.xib文件。在两个.xib文件中,我都有一个UIView。我该如何制作它以便您可以在这两个视图之间滑动?例如,应用程序在第一个视图上打开,然后从右向左滑动,出现下一个视图。我希望在滑动照片时,滑动的动画就像照片应用程序的动画一样。

I now have two view controllers, each with a .h file, a .m file, and a .xib file. In both the .xib files, I have a UIView. How can I make it so that you can swipe between these two views? For example, the app opens on the first view, then you swipe from the right to the left and the next view appears. I want the animation of the swiping to be like that of the Photos app when swiping through photos.

推荐答案

虽然你可以自己实现这个(用结合的定制容器视图控制器 UIPanGestureRecognizer UIScrollView ),如果使用iOS 6及更高版本,最简单的方法是使用页面视图控制器使用滚动过渡样式。

While you could implement this yourself (with custom container view controller in conjunction with UIPanGestureRecognizer or UIScrollView), if using iOS 6 and later, the easiest way will be to use a Page View Controller with "scroll" transition style.

考虑这个故事板:

它包含一个页面视图控制器和两个两页的场景。第1页的故事板标识符为一个,基类为 PageOneViewController 。第2页的故事板标识符为两个,基类为 PageTwoViewController

It consists of a page view controller, and two scenes for two pages. Page 1 has a storyboard identifier of one and a base class of PageOneViewController. Page 2 has a storyboard identifier of two and a base class of PageTwoViewController.

然后我编写了一个 UIPageViewController 子类(也许很明显,这是我为上述故事板的第一个场景指定的类),它具有以下代码:

I then wrote a UIPageViewController subclass (and, perhaps obvious, this is the class I specified for the first scene of the above storyboard), which has the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSource = self;

    [self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"one"]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[PageOneViewController class]])
        return nil;

    return [self.storyboard instantiateViewControllerWithIdentifier:@"one"];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[PageTwoViewController class]])
        return nil;

    return [self.storyboard instantiateViewControllerWithIdentifier:@"two"];
}

您也可以使用NIB执行此操作,但需要编写更多代码。如果你搜索 UIPageViewController XIB ,你可能会得到一些点击。

You could do this with NIBs, too, but it requires writing more code. If you search for UIPageViewController XIB and you'll probably get some hits.

如果你需要支持iOS版本在6.0之前,您必须使用平移手势或滚动视图执行自定义容器方法。

If you need to support iOS versions prior to 6.0, you'll have to do the custom container approach with pan gesture or scroll view.

这篇关于在两个UIViewControllers的视图之间滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆