44

I have a UIStoryboard with different UIViewControllers, I would like to add another UIViewController (like a dashboard) that when the user swipe the ipad from left the dashboard will appear then when he swipe back the current view will be restored.

Is this possible? if yes any hint how to do it or any good tutorials for UIGestureRecognizer?

thank you.

Bhanu
  • 1,249
  • 10
  • 17
baste
  • 827
  • 2
  • 9
  • 18

1 Answers1

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

// SwipeRight

UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];

// Implement Gesture Methods

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer 
{
       //Do what you want here
}

-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer 
{
    //Do what you want here
}

Try this one.

Here is the swift version of above code.

Left Swipe

var swipeleft = UISwipeGestureRecognizer(target: self, action: Selector("swipeleft:"))
swipeleft.direction = .left
view.addGestureRecognizer(swipeleft)

Right Swipe

var swiperight = UISwipeGestureRecognizer(target: self, action: Selector("swiperight:"))
swiperight.direction = .right
view.addGestureRecognizer(swiperight)

Method implementation...

 @objc func swiperight(sender: UITapGestureRecognizer? = nil) {
        // Do what u want here
    }

 @objc func swipeleft(sender: UITapGestureRecognizer? = nil) {
        // Do what u want here
    }
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • I want to hold the ViewController in between the Dismissal How can i do that ?? – Alok Sep 06 '16 at 20:32
  • didn't get your question exactly . You wanted to add animation while swiping ?? – Jitendra Sep 07 '16 at 07:10
  • Sorry if i did not made myself clear , Here is My question "i want to add animation while swapping , like in many other application when we drag to right on the screen ,Active screen fades away and when swipe is complete it closes, and the bottom controller become active . now using the above code as soon as i start swipe it gets swiped instead of being smooth and slow .. – Alok Sep 07 '16 at 07:55
  • 1
    @ChaubeyJi Use UIPanGestureRecognizer instead of SwipeGesture. – Manoj Sep 30 '16 at 06:49
  • How to use it in horizontal stackview by adding views runtime? – Pinank Lakhani Dec 22 '16 at 03:46
  • 1
    the moronic platform holder's documentation is so useless that after reading it so many times, I still can't figure out how to implement it and have to resort to googling and finally found the answer in stackoverflow – Zennichimaro Jan 11 '18 at 04:50
  • 1
    @Zennichimaro hope this answer helps. – Jitendra Jan 11 '18 at 13:29