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
}
Related videos on Youtube
Author by
baste
Updated on March 29, 2020Comments
-
baste about 4 years
I have a UIStoryboard with different
UIViewControllers
, I would like to add anotherUIViewController
(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 almost 11 yearsHere is the answer to your problem. stackoverflow.com/a/6326865/1965698
-
baste almost 11 yearsThank you but i am looking for a way in storyboard approach,
-
Jitendra almost 11 yearscan you tried my solution....
-
-
Alok over 7 yearsI want to hold the ViewController in between the Dismissal How can i do that ??
-
Jitendra over 7 yearsdidn't get your question exactly . You wanted to add animation while swiping ??
-
Alok over 7 yearsSorry 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 over 7 years@ChaubeyJi Use UIPanGestureRecognizer instead of SwipeGesture.
-
Pinank Lakhani over 7 yearsHow to use it in horizontal stackview by adding views runtime?
-
Zennichimaro over 6 yearsthe 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 over 6 years@Zennichimaro hope this answer helps.