How to share views using Xcode swipe gestures

I use Xcode to develop a Cocoa touchscreen application for the iOS platform, but I'm having trouble finding how to implement a realistic gesture that allows the user to swipe left or right to switch to the new ViewController (nib / xib file). I did a swapView IBAction with a button and a modal transition, and I read about Apple TouchGestureRecognizer , but I don’t know how to implement a swipe action that will change the look.

I DO NOT want to use scrolling, since I have dozens of view controllers that I want the user to scroll through.

Here is an example:

First View Controller.xib: SwipeRight - transition to the second View Controller.xib

Second View Controller.xib:
SwipeLeft - Go to the first View Controller.xib
SwipeRight - transition to the third View Controller.xib

etc.

I have not used UISwipe / Touch Gestures before, but I used the IBAction method to switch views using a button with Modal Transitioning (see below):

 -(IBAction)swapViews; { SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil]; second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentModalViewController:second2 animated:YES]; [second2 release]; } 

Does the napkin use a similar method, formatted differently? If so, how can I sort and format it.

thanks

Edit - answer as a comment on the question

Put it in your view

 UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected:)]; swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeRecognizer]; [swipeRecognizer release]; 

Then add a selector by pasting the following code into your main ...

 - (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender { NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:@"NC2ViewController" bundle:nil]; second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:second2 animated:YES]; [second2 release]; } 

Then just make sure you import another ViewView control that you change using

 #import "SecondViewController" 

at the top of your main file. Hope this helps.

Edit end

+4
source share
2 answers

This sounds like the perfect time to use the UIGestureRecognizer or, more specifically, the UISwipeGestureRecognizer .

For more information on how to use them, see the Gesture Recognition section of the Event Handbook.

+4
source

Suppose you want to swipe left to open another view on the right.

In the storyboard, drag the gesture pointer. This will make the icon below the view controller; drag this icon and go to the ViewController you want to go to. This will add segue, select custom segue. Then create the UIStoryboardSegue class. Add the following code:

 - (void)perform { UIViewController* source = (UIViewController *)self.sourceViewController; UIViewController* destination = (UIViewController *)self.destinationViewController; CGRect sourceFrame = source.view.frame; sourceFrame.origin.x = -sourceFrame.size.width; CGRect destFrame = destination.view.frame; destFrame.origin.x = destination.view.frame.size.width; destination.view.frame = destFrame; destFrame.origin.x = 0; [source.view.superview addSubview:destination.view]; [UIView animateWithDuration:0.5 animations:^{ source.view.frame = sourceFrame; destination.view.frame = destFrame; } completion:^(BOOL finished) { UIWindow *window = source.view.window; [window setRootViewController:destination]; }]; } 
0
source

All Articles