UITapGuestureRecognizerではbigin,cancellは取れない

UITapGuestureRecognizerでtouchesBegan, touchesCancelled, touchesEndedの3つのイベントを使い分けたいなぁとおもって調べた結果できないことがわかりました😇

UITapGuestureRecognizerで取得できるのはtapEndedの判定1回だけです。stateで分けてもendedのところしか止まりません。

そもそもアプローチが間違っており、UILongPressGestureRecognizerで判定するのが正しかったです。

    @IBAction func onTap(_ sender: UILongPressGestureRecognizer) {
        switch sender.state {
        case .began:
            print("touchesBegan")

        case .ended:
            print("touchesEnded")

        default:
            print("touchedCancelled")
        }
    }

⚠️注意

UILongPressGestureRecognizerをViewにくっつけるとデフォルトのDurationが0.5秒に設定されています。

0.5秒ロングタップをしないと反応しなくなってしまうのでここを0に変えましょう!

参考文献

UIGestureRecognizer の種類