Swipe between two UIViewControllers' views
Asked Answered
D

4

9

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.

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.

Directorate answered 4/8, 2013 at 20:41 Comment(0)
P
17

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.

Consider this storyboard:

pageviewcontroller storyboard

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.

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

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.

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.

Profanity answered 4/8, 2013 at 21:20 Comment(0)
M
4

There are a number of right ways to to rich your goal.

For example:

1) From iOS 6 you can use UIPageViewController with transitionStyle property UIPageViewControllerTransitionStyleScroll

2) You can use UIScrollView with property pagingEnable = YES; and add your views inside it. Also you will need to implement all containers methods addChildViewController:, removeFromParentViewController, willMoveToParentViewController:, didMoveToParentViewController: to deal with with appear/disappear mathods of your controllers

3) you can find third-party solution like:

Manzoni answered 4/8, 2013 at 21:47 Comment(3)
Could you possibly help me out with the scroll view option. As I said, I'm not very good with Objective-C so I don't really know what I'm doing. Could you possibly write some example code of how I could implement the UIScrollView. Again, as i said before, i have two view controllers (I don't know if that is right), with a single view inside of each of them. Thank you! Please ask for more info if you need it!Directorate
There are a lot of tutorials and samples in internet: raywenderlich.com/10518/… eureka.ykyuen.info/2010/05/22/… gabriel-tips.blogspot.com/2012/02/… iosdevnotes.com/2011/03/uiscrollview-pagingManzoni
@Directorate Flop has given you a lot of excellent links. You should go through some (if not all) of those and really understand how they work. If you take the time to research your question by going through some of these links, you'll discover a couple of great approaches in solving your question.Profanity
R
1

Here is a great repo for this:

https://github.com/goktugyil/EZSwipeController

You basically need a UIPageViewController and put your ViewControllers inside that.

Rodenhouse answered 19/11, 2015 at 17:7 Comment(0)
P
-1

you can use this repo .It is so much simpler and easily understandable for RnD. (Swift 3 code)

https://github.com/lakshikabhardwaj/LBViewControllerCollection

     let mainViewController = CPPageMenuVC(nibName: "CPPageMenuVC", bundle: nil)
      let pageMenuarray :[PageModal] = [PageModal(pageTitle: "Cat", pageVC: cpCatVC),PageModal(pageTitle: "Cow", pageVC: cowCX),PageModal(pageTitle: "Chat", pageVC: cpCatVC),PageModal(pageTitle: "ElephantElephant", pageVC: elephantVC)]

      pageMenuVC.pageArray = pageMenuarray
      pageMenuVC.tabHeight = 0    // WITHOUT TOPICS
Precession answered 9/3, 2017 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.