如何检测 iPhone 中的震动
在 iPhone 设备的摇动中,我想要调用一些函数,我不知道如何识别摇动,所以我尝试了一些链接并尝试了这段代码,
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { NSLog(@"called"); [self.view setBackgroundColor:[UIColor greenColor]]; } } - (BOOL)canBecomeFirstResponder { return YES; }
但可惜没有运气,所以你能告诉我如何做同样的事情吗?
谢谢问候
On the shake of the iPhone device i want some function to be called, i dont know how to recognize shake so i tried some link and tried this code
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { NSLog(@"called"); [self.view setBackgroundColor:[UIColor greenColor]]; } } - (BOOL)canBecomeFirstResponder { return YES; }
But alas no luck so can you please let me know how i can do the same
Thanks and Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据上面讨论的评论,我正在回答我的问题,以便其他人可以阅读我
在 UIResponder 类文档中得到的解决方案,据说运动事件只会响应第一个响应者,所以我所做的只是添加一个小的函数,这对我来说很有效,所以这是我的解决方案
现在我仍然无法检测到任何震动运动,所以我所要做的就是使我的视图控制器成为第一响应者,为此这是我使用的代码
,我是完成
这是我想出的解决方案
谢谢和问候
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
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
and i was done
This was the solution that i came up with
Thanks and Regards
您可以执行以下操作...
首先...
在应用程序的委托中设置 applicationSupportsShakeToEdit 属性:
其次...
在视图中添加/覆盖 canBecomeFirstResponder、viewDidAppear: 和 viewWillDisappear: 方法控制器:
第三...
将motionEnded方法添加到您的视图控制器:
如果第一个答案没有,并且这只是快速键入而不是,那么这应该可以工作已测试:)
You could do something like this...
Firstly...
Set the applicationSupportsShakeToEdit property in the App's Delegate:
Secondly...
Add/Override canBecomeFirstResponder, viewDidAppear: and viewWillDisappear: methods in your View Controller:
Thirdly...
Add the motionEnded method to your View Controller:
That should work if the first answer did not and this is only quickly typed not tested:)
要全局处理应用程序所有屏幕上的抖动,您可以子类化 UIWindow 并实现 MotionEnded,因为事件将在响应者链中向上冒泡,直到到达窗口对象。使用 Xamarin 的示例:
接下来,在 AppDelegate 中,实现 window 方法以返回 UIWindow 子类的实例。使用 Xamarin 的示例:
瞧,摇动是在整个应用程序中处理的。
To globally handle shake across all screens of your app, you can subclass UIWindow and implement motionEnded, since the event will bubble up the responder chain until it reaches the window-object. Example using Xamarin:
Next, in AppDelegate, implement the window method to return an instance of your UIWindow subclass. Example using Xamarin:
Voilà, shake is handled across the whole app.
有了SSEventListener,事情变得更容易。您的视图控制器不再需要重写
motionEnded:withEvent:
了!使用SSEventListener
,您可以像这样简单地监听摇动事件:[viewController ss_setShakeEventListener:^{ ... }];
Things become easier with SSEventListener. Your view controller doesn't need to override
motionEnded:withEvent:
any more! WithSSEventListener
, you can listener for shake event as simple as this:[viewController ss_setShakeEventListener:^{ ... }];