Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active December 17, 2021 12:32
Show Gist options
  • Save KentarouKanno/a9ac05a12f821f010c4c to your computer and use it in GitHub Desktop.
Save KentarouKanno/a9ac05a12f821f010c4c to your computer and use it in GitHub Desktop.
UIGestureRecognizer

UIGestureRecognizer

★ タップジェスチャー

// タップジェスチャーを生成
let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:")

// タップ回数を設定 Default: 1
tapGesture.numberOfTapsRequired = 2

// タッチの本数を設定 Default: 1
tapGesture.numberOfTouchesRequired = 3

// ジェスチャーをViewに追加する
self.view.addGestureRecognizer(tapGesture)

// タップ時に呼ばれる関数
func tapGesture(sender: UITapGestureRecognizer) {
    
    // タップ回数を取得
    var tapCount = sender.numberOfTapsRequired

    // タッチの本数を取得
    var tapNum = sender.numberOfTouchesRequired
}

★ ピンチジェスチャー

// ピンチジェスチャーを生成
let pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchGesture:")

// ジェスチャーをViewに追加する
self.view.addGestureRecognizer(pinchGesture)

// ジェスチャー発生時によばれる関数
func pinchGesture(sender: UIPinchGestureRecognizer) {

    // ピンチジェスチャー発生時から、どれだけ拡大率が変化したかを取得する
    // 2本の指の距離が離れた場合には、1以上の値、近づいた場合には、1以下の値が取得できる
    let scale = sender.scale

    // ピンチの速度を取得する早いほど値が大きくなる(+は離れた場合 -は近づいた場合)
    let velocity = sender.velocity

    println("scale = \(scale), velocity = \(velocity)")
    //=> scale = 1.13781615915461, velocity = 1.24166285643279
}

★ スワイプジェスチャー生成

let swipeGesture = UISwipeGestureRecognizer(target: self, action: "swipeGesture:")

// 認識するタッチの本数を設定 Default: 1
swipeGesture.numberOfTouchesRequired = 2;

// 認識するジェスチャーの方向を設定する(default: Right)
swipeGesture.direction = UISwipeGestureRecognizerDirection.Left

// ジェスチャーをViewに追加する
self.view.addGestureRecognizer(swipeGesture)

★ スワイプジェスチャー呼び出し

func swipeGesture(sender: UISwipeGestureRecognizer) {

    // ジェスチャーの方向によって処理を分ける場合
    switch sender.direction {
    
    case UISwipeGestureRecognizerDirection.Right:
       print("Swiped Right")

    case UISwipeGestureRecognizerDirection.Left:
        print("Swiped Left")
 
    case UISwipeGestureRecognizerDirection.Up:
        print("Swiped Up")

    case UISwipeGestureRecognizerDirection.Down:
        print("Swiped Down")

    default:
        break
    }

    // タッチの本数を取得
    let tapNum = sender.numberOfTouchesRequired
}

★ UISwipeGestureRecognizerDirection

/*
UISwipeGestureRecognizerDirection.Right
UISwipeGestureRecognizerDirection.Left
UISwipeGestureRecognizerDirection.Up
UISwipeGestureRecognizerDirection.Down
*/

★ ロングプレスジェスチャー生成

// ロングプレスジェスチャーを生成
let longPressGesture = UILongPressGestureRecognizer(target: self, action: "longPressGesture:")

// タップ回数を設定 Default: 0 ※1回のタップで長押し
longPressGesture.numberOfTapsRequired = 1
 
// タッチの本数を設定 Default: 1
longPressGesture.numberOfTouchesRequired = 2

// 長押しが認識される時間を設定 Default: 0.5
longPressGesture.minimumPressDuration = 0.8

// 長押し中に動いても許容されるピクセル数を設定 Default: 10
longPressGesture.allowableMovement = 20

// ジェスチャーをViewに追加する
self.view .addGestureRecognizer(longPressGesture)

★ ロングプレスジェスチャー呼び出し

func longPressGesture(sender: UILongPressGestureRecognizer) {
  
    // ロングプレスが開始したことを検知
    if(sender.state == UIGestureRecognizerState.Began){
        print("longTap Began")
    }

    // 指が離れたことを検知
    if(sender.state == .Ended){
        print("longTap End")
    }
}

★ UIGestureRecognizerState

/*
UIGestureRecognizerState.Possible
UIGestureRecognizerState.Began
UIGestureRecognizerState.Changed
UIGestureRecognizerState.Ended
UIGestureRecognizerState.Cancelled
UIGestureRecognizerState.Failed
*/

★ パンジェスチャー生成

let panGesture = UIPanGestureRecognizer(target: self, action: "panGesture:")
 
// 認識本数の最小値を設定 Default: 1
panGesture.minimumNumberOfTouches = 2

// 認識本数の最大値を設定 Default: UINT_MAX
panGesture.maximumNumberOfTouches = 4

// ジェスチャーをViewに追加する
self.view.addGestureRecognizer(panGesture)

★ パンジェスチャー呼び出し

func panGesture(sender: UIPanGestureRecognizer) {
    // ドラッグ中何度も呼び出される

    // パンジェスチャーで移動した距離を取得する
    var location = sender.translationInView(self.view)

    // パンジェスチャーの速度を取得する
    var velocity = sender.velocityInView(self.view)

    print("pan translation = \(NSStringFromCGPoint(location)),pan velocity = \(NSStringFromCGPoint(velocity))")
    //=> pan translation = {76.166664123535156, 27},pan velocity = {27.408207228699894, 2.5114973293495244}
}

★ ローテートジェスチャー

let rotateGesture = UIRotationGestureRecognizer(target: self, action: "rotateGesture:")

// ジェスチャー発生時によばれる関数
func rotateGesture(sender: UIRotationGestureRecognizer){

    // 回転した量を取得する、ここで取得出来るのはradiansという単位の値
    let rotation = sender.rotation
        
    // ローテートの速度を取得する早いほど値が大きくなる(+は右回転 -は左回転の場合)
    let velocity = sender.velocity
        
    print("rotation = \(rotation), velocity = \(velocity)")
    //=> rotation = -0.882487973395841, velocity = -0.623222512375213
}

★ エッジパンジェスチャー

let edgePanGesture = UIScreenEdgePanGestureRecognizer(target: self, action: "edgePanGesture:")

// 認識本数の最小値を設定 Default: 1
edgePanGesture.minimumNumberOfTouches = 1

// 認識本数の最大値を設定 Default: UINT_MAX
edgePanGesture.maximumNumberOfTouches = 2

// 認識するエッジの方向を設定
edgePanGesture.edges = UIRectEdge.Right

// ジェスチャーをViewに追加する
self.view.addGestureRecognizer(edgePanGesture)

// ジェスチャー発生時によばれる関数
func edgePanGesture(sender: UIScreenEdgePanGestureRecognizer){

    if (sender.edges == UIRectEdge.Right) {

        // エッジパンが右の場合の処理
    }
}

★ UIRectEdge

/*
UIRectEdge.None
UIRectEdge.Left
UIRectEdge.Right
UIRectEdge.Top
UIRectEdge.Bottom
UIRectEdge.All
*/

★ UIPanGestureRecognizerでViewを移動させる

var startPoint:CGPoint!
var endPoint:CGPoint!

@IBAction func panLabel(sender: UIPanGestureRecognizer) {
    
    let state = sender.state
    let translation = sender.translationInView(view)
    print("state = \(state.rawValue) translation = \(translation)")
    
    switch state {
    case .Began:
        startPoint = sender.view!.center
        print("startPoint = ",startPoint)
    case .Ended:
        endPoint = sender.view!.center
        print("lastPoint = ",endPoint)
    default:
        sender.view!.center.x = startPoint.x + translation.x
        sender.view!.center.y = startPoint.y + translation.y
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment