LoginSignup
0
0

More than 1 year has passed since last update.

上下左右のスワイプを検知させる (UISwipeGestureRecognizer)

Posted at

今回の内容

  • 昨日の記事で書いた上下左右のスワイプを検知するようにする。

コードと簡単解説

  • 上下左右のスワイプを検知させるために、インスタンスを1つずつ作成します。
  • 作成したインスタンスを1つずつ.addGestureRecognizer(インスタンス)します。
    override func viewDidLoad() {
        super.viewDidLoad()

        let downSwipeDetection = UISwipeGestureRecognizer(target: self, action: #selector(downScreenSwipe))
        downSwipeDetection.direction = .down
        view.addGestureRecognizer(downSwipeDetection)

        let leftSwipeDetection = UISwipeGestureRecognizer(target: self, action: #selector(leftScreenSwipe))
        leftSwipeDetection.direction = .left
        view.addGestureRecognizer(leftSwipeDetection)

        let rightSwipeDetection = UISwipeGestureRecognizer(target: self, action: #selector(rightScreenSwipe))
        rightSwipeDetection.direction = .right
        view.addGestureRecognizer(rightSwipeDetection)

        let upSwipeDetection = UISwipeGestureRecognizer(target: self, action: #selector(upScreenSwipe))
        upSwipeDetection.direction = .up
        view.addGestureRecognizer(upSwipeDetection)

    }

    @objc func downScreenSwipe(sender:UISwipeGestureRecognizer){

        //スワイプ検知時の処理
    }

    @objc func leftScreenSwipe(){

        //スワイプ検知時の処理 
    }

    @objc func rightScreenSwipe(){

        //スワイプ検知時の処理
    }

    @objc func upScreenSwipe(){

        //スワイプ検知時の処理
    }

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
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
0