In my iPhone app, I require to recognize the swipe gesture made by the user on the view.
I want the swipe gestures to be recognized and perform a function on swipe.
I need that the view should horizontally slide and show another view as a user makes a swipe gesture.
What needs to be done?
How do I recognize it?
If You know how it works, but still need a quick example, here it is! (it will become handy at least for me, when I will need copy-paste example, without trying remembering it)
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething)];
[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:mSwipeUpRecognizer];
and in .h file add:
<UIGestureRecognizerDelegate>
Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videos on the subject even. Sessions 120 and 121. :)
The following link below redirects you to a video tutorial which explains you how to detect swipes on the iPhone in Objective-C:
UISwipeGestureRecognizer Tutorial (Detecting swipes on the iPhone)
Code sample below, to achieve that in Swift:
You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
case UISwipeGestureRecognizerDirection.Down:
println("Swiped down")
default:
break
}
}
}
Swift 5 version:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)
Related
I have tested the following code, and only the selectors for the pan, tap, and long press recognizers activate. I have tried adding the recognizers inside the UIView subclass and adding the recognizers as parameters. My only guess would be that the pan, tap, and long press are somehow gobbling up all of the touches, but I would expect only one of them to work in that case. Since the same procedures are followed for all recognizers, I don't really understand what the problem would be. Thanks in advance for any help.
init(level: LevelView) {
self.level = level
left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
up = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
down = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown))
tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
pan = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
long = UILongPressGestureRecognizer(target: self, action: #selector(long(_: )))
long.minimumPressDuration = 0.3
long.allowableMovement = 30
left.direction = .left
right.direction = .right
up.direction = .up
down.direction = .down
for rec in [up, down, right, left, tap, long, pan] {
rec!.cancelsTouchesInView = false
level.addGestureRecognizer(rec!)
}
}
Update:
The Pan recognizer was immediately recognizing the touches and not passing them to the swipe recognizers. Since I wanted the swipe recognizers to take precedence, I added the condition that all of the swipe gestures fail before the pan recognizer could recognize. That worked. Thank you to Matt for the helpful tip.
My collection view is working great. It shows a grid of photos and lists hundreds of them. You can swipe vertically to scroll through them all. Life is good. However, I now have a new requirement. I need to be able to detect when the user is swiping left or right. I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. Any ideas?
In swift?
If it helps for reference heres a link to my Github project.
https://github.com/StarShowsStudios/GodCards
If you open the project in Xcode you can see the detail view controller. It gets its information from a plist file called cards according to the selected collection cell controller
You can add left and right swipe gesture recognizer to detect left and right swipe.
override func awakeFromNib() {
super.awakeFromNib()
// Add Left Swipe Gesture
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.addGestureRecognizer(swipeLeft)
// Add Right Swipe Gesture
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.addGestureRecognizer(swipeRight)
}
// This function detects Swipe direction and perform action
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("Swiped right")
rightSwipeAction()
case UISwipeGestureRecognizerDirection.Left:
print("Swiped left")
leftSwipeAction()
default:
break
}
}
}
Is there a way to detect up & down swipes on TableView in a ViewController?
in ViewDidLoad();
let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
upSwipe.direction = .Up
downSwipe.direction = .Down
tableView.addGestureRecognizer(upSwipe)
tableView.addGestureRecognizer(downSwipe)
Unfortunately, this doesn't work.
func swipeDown() {
print("A")
}
nothing returned.
What is the appropriate way to detect gestures on TableView (not cell/as a whole)?
Implement UIScrollViewDelegate
Define scrollViewDidScroll(_:)
Set your tableview's scrollview's delegate to whatever object you've got the above defined in.
Exactly what the title implies. How do the gesture recognizers work, specifically UIGestureRecognizer. Here is a small snippet of my code
var keyboardDismiser: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
noteView.addGestureRecognizer(keyboardDismiser)
and
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing()
}
My goal is to dismiss the keyboard when switching from view to view in a UIScrollView with 3 pages. What am I doing wrong? There isn't much documentation on this in Swift.
First you setting the recognizer on the note view. It will only be active on the note view.
In addition, you are not setting direction correctly. You are setting then changing the it's value. To set it to both right and left, you use the | operator. Also direction knows it a UISwipeGestureRecognizerDirection so you don't need specify that.
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
self.view.addGestureRecognizer(keyboardDismiser)
Finally, I would use endEditing() instead of resignFirstResponder().
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing(true)
}
Hope that helps.
I believe that selectors in Swift do not need the : at the end; they're just a string with the name of the function: gestureRecognizer. So this is what you should have:
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer")
Relevant question here.
This question already has answers here:
Views Navigation Using Swipe Gesture
(5 answers)
Closed 9 years ago.
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.
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
}