iOS

UIGestureRecognizer (Swipe, Pan, ScreenEdgePan, Long Press)

JoonSwift 2020. 12. 5. 03:00

UISwipeGestureRecognizer

class UISwipeGestureRecognizer : UIGestureRecognizer

UISwipeGestureRecognizer는 원하는 Swipe Gesture를 만들어 줘야 합니다. 예를들면 다음과 같이 작성할 수 있습니다.

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeRight(_:)))
swipeRight.direction = .right

전체적인 코드를 작성해 보면 다음과 같습니다.

    @IBOutlet var swipeView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let swipeRight = UISwipeGestureRecognizer(target: self,
                                                  action: #selector(handleSwipeRight(_:)))
        let swipeLeft = UISwipeGestureRecognizer(target: self,
                                                 action: #selector(handleSwipeLeft(_:)))
        let swipeUp = UISwipeGestureRecognizer(target: self,
                                               action: #selector(handleSwipeUp(_:)))
        let swipeDown = UISwipeGestureRecognizer(target: self,
                                                 action: #selector(handleSwipeDown(_:)))
        swipeRight.direction = .right
        swipeLeft.direction = .left
        swipeUp.direction = .up
        swipeDown.direction = .down
        
        self.swipeView.gestureRecognizers = [swipeUp, swipeDown, swipeLeft, swipeRight]
    }
        
    @objc func handleSwipeRight(_ sender: UISwipeGestureRecognizer) {
        print("swipe Right!")
    }
    @objc func handleSwipeLeft(_ sender: UISwipeGestureRecognizer) {
        print("swipe Left!")
    }
    @objc func handleSwipeUp(_ sender: UISwipeGestureRecognizer) {
        print("swipe Up")
    }
    @objc func handleSwipeDown(_ sender: UISwipeGestureRecognizer) {
        print("swipe Down")
    }

UIPanGestureRecognizer

class UIPanGestureRecognizer : UIGestureRecognizer

The user must press one or more fingers on a view while panning it

사용자는 하나 또는 여러개의 손가락을 panning할 때 사용해야 합니다. panning은 드래그를 뜻하며 panning역시 연속적인 동작입니다.

    @IBOutlet var panView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
        self.panView.addGestureRecognizer(pan)
    }
    
    @objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
        let translation = sender.translation(in: view)
        guard let gestureView = sender.view else {
            return
        }
        gestureView.center = CGPoint(x: gestureView.center.x + translation.x,
                                     y: gestureView.center.y + translation.y)
        
        sender.setTranslation(.zero, in: view)
    }

UIScreenEdgePanGestureRecognizer

class UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer

화면의 Edge 부분에서 드래그를 하는 Gesture를 인식하는 Gesture Recognizer입니다. 때때로 이 방법을 사용하여 ViewController 전환을 시도하기도 합니다.

    @IBOutlet var screenEdgePanView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let screenEdgePan = UIScreenEdgePanGestureRecognizer(target: self,
                                                             action: #selector(handleScreenEdgePan))
        screenEdgePan.edges = .left
        self.screenEdgePanView.addGestureRecognizer(screenEdgePan)
    }
    
    @objc func handleScreenEdgePan(_ sender: UIScreenEdgePanGestureRecognizer) {
        let translation = sender.translation(in: screenEdgePanView)
        print("Translation : \(translation)")
    }

UILongPressGestureRecognizer

class UILongPressGestureRecognizer : UIGestureRecognizer

UILongPressGestureRecognizer는 사용자가 하나 또는 여러개의 손가락으로 최소 시간동안 손가락을 고정하고 있을 때를 인식하는 Gesture Recognizer이다. 

    @IBOutlet var longPressView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
        longPress.numberOfTapsRequired = 1
        longPress.minimumPressDuration = 0.01
        self.longPressView.addGestureRecognizer(longPress)
    }
    
    @objc func handleLongPress(_ sender: UILongPressGestureRecognizer) {
        print(sender.state)
    }

🙏