How to recognize swipe in all 4 directions
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()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .right:
print("Swiped right")
case .down:
print("Swiped down")
case .left:
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}
Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.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:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}
How to recognize swipe in all 4 directions?
You set the direction like this
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
Swipe.direction = (UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionDown |
UISwipeGestureRecognizerDirectionUp);
That's what the direction will be when you get the callback, so it is normal that all your tests fails. If you had
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction | UISwipeGestureRecognizerDirectionLeft )
NSLog(@" *** SWIPE LEFT ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionRight )
NSLog(@" *** SWIPE RIGHT ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionDown )
NSLog(@" *** SWIPE DOWN ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionUp )
NSLog(@" *** SWIPE UP ***");
}
The tests would succeed (but the would all succeed so you wouldn't get any information out of them). If you want to distinguish between swipes in different directions you will need separate gesture recognizers.
EDIT
As pointed out in the comments, see this answer. Apparently even this doesn't work. You should create swipe with only one direction to make your life easier.
Swift 4, passing UISwipeGestureRecognizer direction to the handler function
you need to create the two separate gesture for each swipe event,reason is actually works in single event, for e.g
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
playCardView.addGestureRecognizer(leftSwipe)
playCardView.addGestureRecognizer(rightSwipe)
}
When a gesture is detected the moveToNextItem method is called, implement this method.
@objc func moveToNextItem(_ sender:UISwipeGestureRecognizer) {
switch sender.direction{
case .left:
//left swipe action
case .right:
//right swipe action
default: //default
}
}
Swift, swipe gesture recognizer direction doesn’t work, can’t seem to get the direction
You need to detect one direction at a time. I would also suggest to add a method to control each swipe direction. For example.
func setUpGestures() {
// Gesture that define a left swipe.
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(Scene.swipeLeft))
swipeLeft.direction = .left
view?.addGestureRecognizer(swipeLeft)
// Do the same for the rest of the directions.
}
@objc func swipeLeft() {
// Do something when the user swipe left.
}
Hope it helps!
how detect swipe gesture direction?
The direction
property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.
The easiest would be to use two separate gesture recognizers instead. You could also inspect the location of the touch when the gesture starts and when it ends with the locationInView:
method.
How to detect Swiping UP, DOWN, LEFT and RIGHT with SwiftUI on a View
Based on Benjamin's answer this is a swiftier way to handle the cases
.gesture(DragGesture(minimumDistance: 3.0, coordinateSpace: .local)
.onEnded { value in
print(value.translation)
switch(value.translation.width, value.translation.height) {
case (...0, -30...30): print("left swipe")
case (0..., -30...30): print("right swipe")
case (-100...100, ...0): print("up swipe")
case (-100...100, 0...): print("down swipe")
default: print("no clue")
}
}
)
Related Topics
@Property Retain, Assign, Copy, Nonatomic in Objective-C
Best Way to Add License Section to iOS Settings Bundle
Positioning Mkmapview to Show Multiple Annotations at Once
Drawing Uibezierpath on Code Generated Uiview
How to Save Local Data in a Swift App
How to Add a Border Just on the Top Side of a Uiview
How to Make App Fully Working Correctly for Autorotation in iOS 6
Change Color of Certain Pixels in a Uiimage
How to Enable_Bitcode in Xcode 7
Error Opening iOS Simulator with iOS 8 Beta Version: "Unable to Boot the iOS Simulator"
Converting Hex String to Nsdata in Swift
Why Is Glreadpixels() Failing in This Code in iOS 6.0
How to Modify Swift_Module_Name
Picking Two Different Images in the Same View Controller Using Imagepickercontroller in Swift
Uitextfield's Numerical Pad: Dot Instead of Comma for Float Values