UISwipeGestureRecognizerでスワイプでのジェスチャーイベントを追加できますが、今回はそれをStoryboradから追加する方法です。
今回、上下左右のスワイプイベントをするとログ出力するようにしました。
Swiftコード
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func leftSwiped() {
NSLog("leftSwiped");
}
@IBAction func rightSwiped() {
NSLog("rightSwiped");
}
@IBAction func upSwiped() {
NSLog("upSwiped");
}
@IBAction func downSwiped() {
NSLog("downSwiped");
}
}
StoryboardからSwipe Gesture Recognizerを追加
1. Xcode右下のコンポーネント一覧から「Swipe Gesture Recognizer」をViewControllerへドラッグ
2. 追加したSwipeGestureRecognizerにDirctionを設定
3. 追加したSwipeGestureRecognizerにSelectorを設定
コードを書かずに設定できるので、コード部分がスッキリしますね。