今回の内容
-
.isUserInteractionEnabled
でタップや長押しやスワイプ時の処理をさせない。
コードと簡単解説
- これだけでviewに対するタップ、長押し、スワイプ時の処理を働かせることができなくなります。
view.isUserInteractionEnabled = false
使ってみる
- viewを5回タップしたら
@objc func screenTap(){}
の処理が働かなくなります。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var detectionResultLabel: UILabel!
var tapCount = 0
override func viewDidLoad() {
super.viewDidLoad()
let tapDetection = UITapGestureRecognizer(target: self, action: #selector(screenTap))
view.addGestureRecognizer(tapDetection)
}
@objc func screenTap(){
if tapCount == 5{
self.view.isUserInteractionEnabled = false
}else{
tapCount += 1
detectionResultLabel.text = "\(tapCount)回、画面をタップしました"
}
}
}
終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。