Add UIGestureRecognizer to swipe left to right right to left my views

53,935
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
    }
Share:
53,935

Related videos on Youtube

baste
Author by

baste

Updated on March 29, 2020

Comments

  • baste
    baste about 4 years

    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.

    • Hawk-Eye
      Hawk-Eye almost 11 years
      Here is the answer to your problem. stackoverflow.com/a/6326865/1965698
    • baste
      baste almost 11 years
      Thank you but i am looking for a way in storyboard approach,
    • Jitendra
      Jitendra almost 11 years
      can you tried my solution....
  • Alok
    Alok over 7 years
    I want to hold the ViewController in between the Dismissal How can i do that ??
  • Jitendra
    Jitendra over 7 years
    didn't get your question exactly . You wanted to add animation while swiping ??
  • Alok
    Alok over 7 years
    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 ..
  • Manoj
    Manoj over 7 years
    @ChaubeyJi Use UIPanGestureRecognizer instead of SwipeGesture.
  • Pinank Lakhani
    Pinank Lakhani over 7 years
    How to use it in horizontal stackview by adding views runtime?
  • Zennichimaro
    Zennichimaro over 6 years
    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
  • Jitendra
    Jitendra over 6 years
    @Zennichimaro hope this answer helps.