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
}
}
}
Long Press Gesture WatchKit
A Long Press Gesture Recognizer has to be dragged into a Storyboard from the Object library. From there, you Control-drag it to code to set an action connection, and handle long presses within that method.
Related Topics
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
Create a Generic Swift Function to Return an Array of Core Data Entities
Binary to Hexadecimal in Swift
How to Use The Snapchat Sdk (Snapkit) with Swiftui
Difference Between Userdefaults() and Userdefaults.Standard
Alamofire Post Request with JSON Encoding
How to Access The Firebase Topics a User Is Subscribed To
Error Loading Media Sources in Mlmedialibrary
Xcode 13.1 Bug Editing Dependency as Local
Swift & Firebase: Detect Revoked Token
Is The for Loop Condition Evaluated Each Loop in Swift
Position of Mouse Click Relative to Scene, Not Window
Changing Value of Character Using Ascii Value in Swift
Swift Short Syntax of Execution
Swiftui - Button - How to Pass a Function (With Parameters) Request to Parent from Child
Swift - Boundingbox Cause Exc_Bad_Access (Code=1)