0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift:UISwipeGestureRecognizer備忘録

Posted at

##バージョン

・Xcode11.4.1
・Swift5

##概要
imageView上のスワイプを検知して、ラベルに方向を表示させる。
今回は上のみ検知。

##コード

test.swift
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var animationView: UIImageView!

override func viewDidLoad() {
  super.viewDidLoad()
   //UISwipeGestureRecognizerのインスタンス作成
    let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeLabel(sender:)))
  //UISwipeGestureRecognizerで検知する方向を設定
    swipeRecognizer.direction = .up
    //isUserInteractionEnabledをtrueにしないととエラー出ないけれど、animationViewをスワイプしても認識されない
    animationView.isUserInteractionEnabled = true
    //uiimageにスワイプの感知を追加
    animationView.addGestureRecognizer(swipeRecognizer)
}

    //スワイプされたときに動く関数
    @objc func swipeLabel(sender:UISwipeGestureRecognizer) {
        testLabel.text = "上"
        print("up")
        } 
}

##参考にさせて頂いた記事
UITapGestureRecognizer が反応しない

【Swift】Swipe Gesture Recognizerの使い方。上下左右のスワイプを検知する。

UISwipeGestureRecognizerの罠と左右スワイプの実装

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?