iOS: Touch, Tap, Pinch, Rotate, Custom Gestures

UIGestureRecognizer Tutorial

Prafulla Singh

--

1. Single Swipe Gestures

final class SingleSwipeExampleViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)

let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeUp)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)

}


@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == .right {
self.logLabel.text = "Swipe Right"
}
else if gesture.direction == .left {
self.logLabel.text = "Swipe Left"
}
else if gesture.direction == .up {
self.logLabel.text = "Swipe up"
}
else if gesture.direction == .down {
self.logLabel.text = "Swipe Down"

}
}

2. Multiple Swipe(s) Gestures

Same as Single Swipe Gestures. along with direction UISwipeGestureRecognizer need numberOfTouchesRequired parameter for number of Fingers.

swipeUp.numberOfTouchesRequired = 2 //number of finger to swipe

3. Single Tap Gestures

final class SingleTapExampleViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
let singleTap = UITapGestureRecognizer.init(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(singleTap)
}

@objc func doSingleTap(gesture: UITapGestureRecognizer) -> Void {
logLabel.text = "Single tap"
}

}

4. Multiple Tap(s) Gestures

Same as Single Tap Gestures. along with direction UITapGestureRecognizer need numberOfTapsRequired parameter for…

--

--