画面操作系(単タップ、長タップ、ダブルタップなど)
UIGestureRecognizer
配下にある指による画面操作系の制御。全部で8個。
・UITapGestureRecognizer
・UIPinch
・UIRotation
・UISwipe
・UIPan
・UIScreenEdgePan
・UILongPress
・UIHover
●基本記法(例:UILongPress)
class GestureViewController: UIViewController, UIGestureRecognizerDelegate {
// view系のUI
let tableView: UITableView!
// viewDidLoad内で宣言する
override func viewDidLoad() {
let longTap = UILongPressGestureRecognizer(target: self
action: #selector(tappedButton))
// delegateは細かい設定をするときには必要なのかもしれない[要調査]
longTap.delegate = self
// view全体に適応させたいとき
self.view.addGestureRecognizer(longTap)
// 特定のviewに適応させたいとき
self.tabelView.addGestureRecognizer(longTap)
}
@objc func tappedButton(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .Began {
print("タップしてます")
} else if gesture.state == .Ended {
print("指離しました")
}
}
}
storyboard上でも紐付けできるらしい。
UIはStoryboardで作ってジェスチャーはコードで紐付けとかできるんだろうか。