Detect Shake Gesture iOS Swift

Detecting shake in AppDelegate

Add the following snippet in your AppDelegate:

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
if motion == .MotionShake {
print("Device shaken")
}
}

Swift 3.0 version:

override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken")
}
}

As for later versions this does not seem to work anymore. You need to add the above code in your view controller instead

How to identify iPhone SHAKE in Swift?]

The main trick is that you need to have some UIView (not UIViewController) that you want as firstResponder to receive the shake event messages. Here's the code that you can use in any UIView to get shake events:

class ShakingView: UIView {

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if event?.subtype == .motionShake {
// Put in code here to handle shake
}
if super.responds(to: #selector(UIResponder.motionEnded(_:with:))) {
super.motionEnded(motion, with: event)
}
}

override var canBecomeFirstResponder: Bool { return true }

You can easily transform any UIView (even system views) into a view that can get the shake event simply by subclassing the view with only these methods (and then selecting this new type instead of the base type in IB, or using it when allocating a view).

In the view controller, you want to set this view to become first responder:

override func viewWillAppear(_ animated: Bool) {
shakeView.becomeFirstResponder()
super.viewWillAppear(animated)
}

override func viewWillDisappear(_ animated: Bool) {
shakeView.resignFirstResponder()
super.viewWillDisappear(animated)
}

Don't forget that if you have other views that become first responder from user actions (like a search bar or text entry field) you'll also need to restore the shaking view first responder status when the other view resigns!

This method works even if you set applicationSupportsShakeToEdit to NO.

For objective c version refer link How do I detect when someone shakes an iPhone?

Detecting Shake Gesture SpriteKit in Swift

Apparently, you can't detect shake events from a SKScene subclass, such as GameScene. However, you can detect them from a view controller, such as GameViewController. When a shake event is triggered, you can call a shake handler in GameScene from the view controller.

In your GameViewController, add this to detect shake events

override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
if let skView = view as? SKView, let scene = skView.scene as? GameScene {
scene.shake()
}
}
}

and add this to GameScene

func shake() {
print("Shake")
}

EDIT

If you need to detect shake events from multiple SKScene subclasses, you can define the following extensions:

extension SKScene {
func shake() {
}
}

extension SKView {
open override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if let scene = self.scene {
scene.shake()
}
}
}

and add the following to the appropriate SKScene subclasses

override func shake() {
print("Shake")
}

How to detect shake motion in iPhone

As per the above comments discussed i am answering my question so that others can read the solution that i have got

In the UIResponder class documentation it is said that the motion events will respond to the first responder only so what i did was add just a small function and that did the trick for me, so here's my solution


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
NSLog(@"called");
[self.view setBackgroundColor:[UIColor greenColor]];
}
}

- (BOOL)canBecomeFirstResponder
{
return YES;
}

Now still i was not able to detect any shake motion so all i had to do was to make my viewcontroller the first responder and for that here's the code that i used


- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}

and i was done

This was the solution that i came up with

Thanks and Regards



Related Topics



Leave a reply



Submit