问题描述
我正在尝试在此页面上实现摇动教程",但我认为我遗漏了一些东西.我将他的加速度计功能复制到 myAppViewController.m 文件中,并在其中放置了一些 nslog,以查看当我使用模拟器摇动"功能时它是否进入该功能.调试控制台中没有显示任何内容.
I'm trying to implement the shake "tutorial" on this page, but I think I'm missing something. I copied his accelerometer function into myAppViewController.m file and put some nslogs in there to see if it even gets into the function when I use the simulators "shake" function. Nothing shows up in the debug console.
http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk
谁能解释我可能遗漏了什么?或者指点我一个教程?
Can anyone explain what I might be missing? Or point me to a tutorial?
我发现了这个,看起来很有希望,但我不知道如何把它放在 UIView 中"如何检测有人摇晃 iPhone?
I found this, which looks promising, but I don't know how to "put it in a UIView" How do I detect when someone shakes an iPhone?
编辑 - 由于接受了答案的建议,现在这是我的工作代码.
EDIT - now here's my working code because of the accepted answer's suggestion.
这是我在 3.0 iphone sdk 中检测摇晃手势的代码.
Here's my code to detect the shake gesture in 3.0 iphone sdk.
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"{motion ended event ");
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"{shaken state ");
}
else {
NSLog(@"{not shaken state ");
}
}
推荐答案
你应该绝对不要直接用你自己的过滤器来监听 UIAccelerometer
来处理抖动事件.这是一项高功率操作,仅应由需要高加速度计采样率的应用程序使用.使用已添加到 UIEvent
的新运动事件:
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一个>
就像触摸一样,动作事件将被传递给第一响应者,然后如果第一响应者没有响应,则沿着响应者链向上传播.UIEvent
将具有类型 UIEventTypeMotion
和子类型 UIEventSubtypeMotionShake
.
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
.
这篇关于如何检测和编程 iPhone 的震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!