0
0

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.

画面タップを検知 復習

Posted at

今回の内容

  • 画面がタップされた時に処理をさせようと思い書こうとしたら、すぐに思い出せなかった為復習です。

コードと簡単解説

  • UITapGestureRecognizer(target: self, action: #selector(メソッド名))でタップ時の処理を作成します。

  • .addGestureRecognizer()でタップした時に処理が働くようになります。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    
    var tapCount = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let tapDetection = UITapGestureRecognizer(target: self, action: #selector(screenTap))
        view.addGestureRecognizer(tapDetection)
    }

    @objc func screenTap(){
        
        tapCount += 1
        label.text = "\(String(tapCount))回、画面をタップしました。"
    }

}

終わり

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?