Swift Viewにジェスチャーリスナーを定義する

個人開発したアプリの宣伝
目的地が設定できる手帳のような使い心地のTODOアプリを公開しています。
Todo with Location

Todo with Location

  • Yoshiko Ichikawa
  • Productivity
  • Free

スポンサードリンク

View.addGestureRecognizer()を使用する。addするGestureRecognizerはUIGestureRecognizerを継承したオブジェクトとなる。


GestureRecognizerの種類
UITapGestureRecognizer : タップ、ダブルタップ
UIPinchGestureRecognizer :  ピンチ
UIPanGestureRecognizer :    ドラッグ
UISwipeGestureRecognizer :  スワイプ
UIRotationGestureRecognizer :   ローテーション
UILongPressGestureRecognizer :  長押し


UISwipeGestureRecognizerで左 -> 右方向のスワイプを検知する
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(doRightAction(sender:)))
swipeGestureRecognizer.direction = .right
self.view.addGestureRecognizer(swipeGestureRecognizer)

//swipe時の処理を記述
@objc func doRightAction(sender:UISwipeGestureRecognizer){
}