当有人摇动iPhone时,我该如何检测?

当有人摇动iPhone时,我该如何检测?

我想在有人摇晃iPhone的时候做出反应。我不在乎他们是如何摇动它的,只是它被有力地挥动了一下。有人知道怎么发现这个吗?



交互式爱情
浏览 568回答 3
3回答

梦里花落0921

在3.0,现在有一个更容易的方法-钩到新的运动事件。主要的诀窍是,您需要一些UIView(而不是UIViewController)作为FirstResponder来接收抖动事件消息。下面是您可以在任何UIView中使用的代码来获得抖动事件:@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您可以轻松地将任何UIView(甚至系统视图)转换为一个视图,只需使用这些方法对视图进行子类(然后选择此新类型而不是IB中的基类型,或者在分配视图时使用它),就可以简单地获得抖动事件。在视图控制器中,您希望将此视图设置为第一个响应者:- (void) viewWillAppear:(BOOL)animated{     [shakeView becomeFirstResponder];     [super viewWillAppear:animated];}- (void) viewWillDisappear:(BOOL)animated{     [shakeView resignFirstResponder];     [super viewWillDisappear:animated];}不要忘记,如果您有其他视图成为用户操作的第一个响应者(如搜索栏或文本输入字段),则还需要在其他视图退出时恢复抖动视图第一响应状态!即使将application ationSupportsShakeToEdit设置为no,此方法也有效。

白衣非少年

从我的狄沙克申请://&nbsp;Ensures&nbsp;the&nbsp;shake&nbsp;is&nbsp;strong&nbsp;enough&nbsp;on&nbsp;at&nbsp;least&nbsp;two&nbsp;axes&nbsp;before&nbsp;declaring&nbsp;it&nbsp;a&nbsp;shake.//&nbsp;"Strong&nbsp;enough"&nbsp;means&nbsp;"greater&nbsp;than&nbsp;a&nbsp;client-supplied&nbsp;threshold"&nbsp;in&nbsp;G's.static&nbsp;BOOL&nbsp;L0AccelerationIsShaking(UIAcceleration*&nbsp;last,&nbsp;UIAcceleration*&nbsp;current,&nbsp;double&nbsp;threshold)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;double &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deltaX&nbsp;=&nbsp;fabs(last.x&nbsp;-&nbsp;current.x), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deltaY&nbsp;=&nbsp;fabs(last.y&nbsp;-&nbsp;current.y), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deltaZ&nbsp;=&nbsp;fabs(last.z&nbsp;-&nbsp;current.z); &nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(deltaX&nbsp;>&nbsp;threshold&nbsp;&&&nbsp;deltaY&nbsp;>&nbsp;threshold)&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(deltaX&nbsp;>&nbsp;threshold&nbsp;&&&nbsp;deltaZ&nbsp;>&nbsp;threshold)&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(deltaY&nbsp;>&nbsp;threshold&nbsp;&&&nbsp;deltaZ&nbsp;>&nbsp;threshold);}@interface&nbsp;L0AppDelegate&nbsp;:&nbsp;NSObject&nbsp;<UIApplicationDelegate>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;histeresisExcited; &nbsp;&nbsp;&nbsp;&nbsp;UIAcceleration*&nbsp;lastAcceleration;}@property(retain)&nbsp;UIAcceleration*&nbsp;lastAcceleration;@end@implementation&nbsp;L0AppDelegate-&nbsp;(void)applicationDidFinishLaunching:(UIApplication&nbsp;*)application&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[UIAccelerometer&nbsp;sharedAccelerometer].delegate&nbsp;=&nbsp;self;}-&nbsp;(void)&nbsp;accelerometer:(UIAccelerometer&nbsp;*)accelerometer&nbsp;didAccelerate:(UIAcceleration&nbsp;*)acceleration&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(self.lastAcceleration)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!histeresisExcited&nbsp;&&&nbsp;L0AccelerationIsShaking(self.lastAcceleration,&nbsp;acceleration,&nbsp;0.7))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;histeresisExcited&nbsp;=&nbsp;YES; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;SHAKE&nbsp;DETECTED.&nbsp;DO&nbsp;HERE&nbsp;WHAT&nbsp;YOU&nbsp;WANT.&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(histeresisExcited&nbsp;&&&nbsp;!L0AccelerationIsShaking(self.lastAcceleration,&nbsp;acceleration,&nbsp;0.2))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;histeresisExcited&nbsp;=&nbsp;NO; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;self.lastAcceleration&nbsp;=&nbsp;acceleration;}//&nbsp;and&nbsp;proper&nbsp;@synthesize&nbsp;and&nbsp;-dealloc&nbsp;boilerplate&nbsp;code@end计量学防止抖动事件多次触发,直到用户停止抖动。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS