今回の内容
- 昨日の記事で書いた上下左右のスワイプを検知するようにする。
コードと簡単解説
- 上下左右のスワイプを検知させるために、インスタンスを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(){
//スワイプ検知時の処理
}
終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。