Swipe Gesture and Motion Shake

Define an UISwipeGestureRecognizer:

1
2
3
4
5
6
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
}

Detailed actions are in the func:

1
2
3
4
5
6
7
8
9
10
func swiped(gesture: UISwipeGestureRecognizer) {  
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("right")
default:
break
}
}
}

To deal with the motion shake of iPhone:

1
2
3
4
5
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
if event?.subtype == UIEventSubtype.MotionShake {
print("The phone has been shaken.”)
}
}