Detect iOS Swift Shake Gesture

I am developing an application with a rigid system, basically, if I return the iPhone to leave my application, I will make a function, if I return the iPhone to the right, another function, with different gestures.

I have no idea how to work with this, I try to search in google, but it doesn’t work, the result is only a touch gesture, not a motion gesture.

Does anyone have a tutorial to help me?

+20
source share
7 answers

Swift3 ios10:

override func viewDidLoad() { super.viewDidLoad() self.becomeFirstResponder() // To get shake gesture } // We are willing to become first responder to get shake motion override var canBecomeFirstResponder: Bool { get { return true } } // Enable detection of shake motion override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) { if motion == .motionShake { print("Why are you shaking me?") } } 
+56
source

Super easy to implement:

1) Let iOS know which view controller is the first in the responder chain:

 override func viewDidLoad() { super.viewDidLoad() self.becomeFirstResponder() } override func canBecomeFirstResponder() -> Bool { return true } 

2) Handle the event in a certain way:

 override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) { if(event.subtype == UIEventSubtype.MotionShake) { print("You shook me, now what") } } 
+16
source

This has been updated for iOS 12 - see Apple Docs. You no longer need to add the comeFirstResponder () method.

You just need to add the motionEnded function to your code.

 override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // code here } 
+4
source

Swift 5

Just add the following methods to the ViewController and execute the code

 override func becomeFirstResponder() -> Bool { return true } override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?){ if motion == .motionShake { print("Shake Gesture Detected") //show some alert here } } 
+4
source

Speedy99 quick response in Objective-C version

 - (void)viewDidLoad { [super viewDidLoad]; [self becomeFirstResponder]; } - (BOOL)canBecomeFirstResponder{ return true; } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if(event.subtype == UIEventSubtypeMotionShake){ NSLog(@"Why are you shaking me?"); } } 
+1
source

Swift 5

 class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.becomeFirstResponder() } override var canBecomeFirstResponder: Bool { get { return true } } override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { if motion == .motionShake { print("shake") } } } 
+1
source

Swift docs: - https://developer.apple.com/documentation/uikit/uiresponder .

To detect motion events, Swift has 3 methods:

  1. func motionBegan () β†’ Informs the recipient that the movement has started

  2. func motionEnded () β†’ Notifies the recipient that the motion event has ended

  3. func motionCancelled () β†’ Notifies the recipient that the motion event has been canceled

For example, if you want to refresh the image after shaking with a gesture

 class ViewController: UIViewController { override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { updateImage() } UpdateImage(){ // Implementation } } 
0
source

All Articles