user964627
user964627

Reputation: 665

Swiping Between Views

Maybe you guys can help me. I have three different views (Current right now in storyboard). I would like the user to swipe between the three views instead of clicking a button. Is there a easy way to enable the swiping and to allow left to right and right to left swiping?

I have been doing some research on Apples Dev. website and found some helpful stuff nothing that really explains switching between views.

Upvotes: 0

Views: 7104

Answers (4)

Jay Imerman
Jay Imerman

Reputation: 4600

What about a UIPageControl? You can use that to detect/control requests to switch between pages, then use the delegate UIControlEventValueChanged event to switch content in the view.

Upvotes: 0

Eliz
Eliz

Reputation: 88

You can consider a scroll view as well. It is good especially if you may need pagination in the end, or if you are using them for images and you may need to zoom in and out.

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

Upvotes: 0

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Some of the ways to switch between views in iOS -

  1. Best is to use a NavigationViewController much tighter memory management.
  2. One can use ModalViewControllers also.
  3. Do a custom presentation.

Of all the three, the first one although is more mem eff. but to animate the transition between views is difficult. So is the second one. The third gives you the maximum flexibility to provide custom animations between view transitions but you need to really take care of memory management yourself.

Now coming back to your particular problem; it seems pretty straightforward. You can take a couple of approaches -

  1. Have a huge UIScrollView & enable only horizontal scroll & paging. Now add your views in this scrollView. upon swipe you get to see your new views.
  2. If scroll view is not what you want & want to have some more customization as to how the swipe happens then you can try to do the following -
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[yourView addGestureRecognizer:swipe];
[swipe release];

With this the swipe gesture is defined on yourView if some one swipes iOS will hit the selector handleViewsSwipe. You handle what happens when a user swipes here.

define the selector as

- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)gesture

Hope this helps..

Upvotes: 2

Alan Moore
Alan Moore

Reputation: 6575

Yes, you just use the UISwipeGestureRecognizer to recognize left and right swipes:

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISwipeGestureRecognizer_Class/Reference/Reference.html

You will need to do something different for pre-iOS 3.2 if that matters to you.

Switching between views is easy enough, you can just build up three different subviews and only make the one you need visible. There are other ways to do it also, depending on the type of material you are using -- I would need more info to give a more specific answer than that.

Upvotes: 1

Related Questions