For swiping in each direction we need to create Swipe gesture recognizer and specify direction that we need for this recognizer. For each direction we need create separate recognizer.
override func viewDidLoad() {
super.viewDidLoad()
// Swipe Left
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipingLeft))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
// Swipe Right
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipingRight))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func swipingLeft(sender: UISwipeGestureRecognizer) {
print("Swipe Left")
}
@objc func swipingRight(sender: UISwipeGestureRecognizer) {
print("Swipe Right")
}
We can make code more universal. When we swiping in different directions we can handle all this swipes in one method and switch between directions.
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self,
action: #selector(self.handleSwipe))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self,
action: #selector(self.handleSwipe))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}
@objc func handleSwipe(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
}
}
}
Really amazing information you have shared i was looking for this kind of unique information please do share that kind of unique information more as you can.
ReplyDelete-Web Development Services
This is an excellent article! Thank you a lot. I'm in desperate need of these suggestions. The in-depth explanation is quite beneficial. It was incredibly encouraging to see how you write. Best Custom Websites
ReplyDeleteYour article is unbelievable with precise knowledge. I can see a lot of research behind that and that is beyond my expectations.
ReplyDeleteCreate Your Own Website \
This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website
ReplyDelete