Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] 제스쳐 동시 인식 처리 방법 (gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)) 본문

iOS 응용 (swift)

[iOS - swift] 제스쳐 동시 인식 처리 방법 (gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:))

jake-kim 2023. 12. 5. 01:55

제스처  동시 인식 활성화 방법

  • 아래처럼 swipeGesturedㅘ panGesture 제스쳐를 view에 추가하면, 디폴트 동작은 추후에 추가한 panGesture만 동작
class ViewController: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
        swipeGestureRecognizer.direction = .up
        view.addGestureRecognizer(swipeGestureRecognizer)

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        view.addGestureRecognizer(panGestureRecognizer)

        swipeGestureRecognizer.delegate = self
        panGestureRecognizer.delegate = self
    }

    @objc func handleSwipe(_ gestureRecognizer: UISwipeGestureRecognizer) {
        print("swipe!!")
    }

    @objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
        print("Pan!")
    }
}

ex) 제스쳐가 중복되어있을때 추후에 추가된 panGesture만 동작

 

  • 이와는 다르게, swipeGesture, panGesture모두 제스처가 동작되게하는 방법은?
    • gestureRecognizer에서 return true를 사용

https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer

 

결과) 

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

 

* 전체 코드: https://github.com/JK0369/ExGestures_

* 참고

- https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer

Comments