如何检测和编程iPhone的抖动 [英] how to detect and program around shakes for the iphone
问题描述
我试图在这个页面上实现教程,但是我觉得我缺少一些东西。我将其加速度计功能复制到myAppViewController.m文件中,并在其中放入一些nslogs,以便在使用模拟器shake功能时是否进入该功能。调试控制台中没有任何内容。
http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk
$ b $任何人都可以解释我可能会丢失什么?或者指向我一个教程?
我发现这个,看起来很有前途,但我不知道如何把它放在UIView
< a href =http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone>如何检测何时有人震动iPhone?
编辑 - 现在这里是我的工作代码,因为接受的答案的建议。
这是我的代码,以检测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
的新动作事件:
就像触摸一样,运动事件将被传递给第一个响应者,然后如果第一个响应者没有响应,则向上移动响应者链。 UIEvent
将具有类型 UIEventTypeMotion
和子类型 UIEventSubtypeMotionShake
。 / p>
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?
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.
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 ");
}
}
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
:
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的抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!