How to Detect When Someone Shakes an iPhone

How do I detect when someone shakes an iPhone?

In 3.0, there's now an easier way - hook into the new motion events.

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:

@implementation ShakingView

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if ( event.subtype == UIEventSubtypeMotionShake )
{
// Put in code here to handle shake
}

if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}

- (BOOL)canBecomeFirstResponder
{ return YES; }

@end

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:

- (void) viewWillAppear:(BOOL)animated
{
[shakeView becomeFirstResponder];
[super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[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.

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?

how to detect and program around shakes for the iphone

You should absolutely not be listening to UIAccelerometer directly with your own filtering to handle shake events. That is a high-power operation and should only be used by apps that need a high accelerometer sampling rate. Use the new motion events instead which have been added to UIEvent:

http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/
EventHandling/EventHandling.html#//apple_ref/doc/uid/TP40007072-CH9-SW24

Just like touches, a motion event will be delivered to the first responder, then travel up the responder chain if the first responder does not respond. The UIEvent will have type UIEventTypeMotion and subtype UIEventSubtypeMotionShake.

How to detect iphone shake gesture one time !

Remove the delegate:

[UIAccelerometer sharedAccelerometer].delegate = nil;

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

iOS: How to identify continuous shaking event

Normally when you shake the device the system will send your view controller the events: motionBegan -> motionEnded.

When you shake continuously, the system send the message motionBegan -> motionCancelled (because it not a single shake anymore) multiple times.

You can try to play with this sequence of event, but I think you have to use the accelerometer, see this answer (diceshaker).

How the shake gesture works in iOS?

it depends on what device you have, but Core Motion is indeed the correct answer.

Devices usually have two ways to go about detecting motion, the accelerometer and the gyroscope.

Not all iOS devices have a gyroscope though. Regardless, Core Motion when enabled tracks the device and allows you to sample from one or both.

Check out apple's docs for a more detailed understanding.



Related Topics



Leave a reply



Submit