// ダブルタップ
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(self.tapDouble(sender:)))
doubleTap.numberOfTapsRequired = 2
self.view.addGestureRecognizer(doubleTap)
// シングルタップ
let singleTap = UITapGestureRecognizer(target: self, action: #selector(self.tapSingle(sender:)))
singleTap.numberOfTapsRequired = 1
singleTap.require(toFail: doubleTap)
self.view.addGestureRecognizer(singleTap)
// ピンチを定義
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchView(sender:)))
self.view.addGestureRecognizer(pinchGesture)
// ローテイトを定義
let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.rotationView(sender:)))
self.view.addGestureRecognizer(rotationGesture)
// エッジを定義
let edgeLeftGesture = UIScreenEdgePanGestureRecognizer(target: self, action:selector(self.edgeLeftView(sender:)))
edgeLeftGesture.edges = .left //左端をスワイプするのを検知する
self.view.addGestureRecognizer(edgeLeftGesture)
let edgeRightGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(self.edgeRightView(sender:)))
edgeRightGesture.edges = .right //右端をスワイプするのを検知する
self.view.addGestureRecognizer(edgeRightGesture)
// スワイプを定義
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(self.leftSwipeView(sender:)))
leftSwipe.direction = .left
self.view.addGestureRecognizer(leftSwipe)
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(self.rightSwipeView(sender:)))
rightSwipe.direction = .right
self.view.addGestureRecognizer(rightSwipe)
// パンを定義
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panView(sender:)))
self.view.addGestureRecognizer(panGesture)
↓
↓
(実行ファンクション)
↓
↓
/// ダブルタップ時に実行される
@objc func tapDouble(sender: UITapGestureRecognizer) {
print(“double”)
}
/// シングルタップ時に実行される
@objc func tapSingle(sender: UITapGestureRecognizer) {
print(“single”)
}
/// ピンチイン・ピンチアウト時に実行される
@objc func pinchView(sender: UIPinchGestureRecognizer) {
// ピンチイン・ピンチアウトの拡大縮小率
print(“pinch/scale: \(sender.scale)”)
// 1秒あたりのピンチの速度(read-only)
print(“pinch/velocity: \(sender.velocity)”)
}
/// ローテイト時に実行される
@objc func rotationView(sender: UIRotationGestureRecognizer) {
print(“rotation/rotation: \(sender.rotation)”)
print(“rotation/velocity: \(sender.velocity)”)
}
/// エッジ時に実行される
@objc func edgeLeftView(sender: UIScreenEdgePanGestureRecognizer) {
print(“edge/left”)
}
@objc func edgeRightView(sender: UIScreenEdgePanGestureRecognizer) {
print(“edge/right”)
}
/// スワイプ時に実行される
@objc func leftSwipeView(sender: UISwipeGestureRecognizer) {
print(“left Swipe”)
}
@objc func rightSwipeView(sender: UISwipeGestureRecognizer) {
print(“right Swipe”)
}
/// パン時に実行される
@objc func panView(sender: UIPanGestureRecognizer) {
print(“パン”)
//移動後の相対位置を取得
let location: CGPoint = sender.translation(in: self.view) //Swift3
print(location)
}