无法在 iPhone 上获取摇动事件
我无法在 iPhone 上获取震动事件。
我在这里关注了其他问题但没有结果。我还尝试遵循 Apple 的 GLPaint 示例,但它看起来与我的源代码完全相同,只有一点点差异。 GLPaint 的源代码 /works/,我的 /doesn't/。
所以,这就是我所拥有的:
Controller.m
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded) name:@"shake" object:nil];
}
ShakingEnabledWindow.m
- (void)shakeEnded {
NSLog(@"Shaking ended.");
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake ) {
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
NSLog(@"Shaken!");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
我的 XIB 有一个窗口,它是一个 ShakingEnabledWindow 和一个对象,即我的控制器。
我在这里没有想法,希望有人能帮我。 :)
I am not being able to get shake events on iPhone.
I have followed other questions here with no result. I also tried following the GLPaint example from Apple, but it seems exactly like my source code, with a small diference. GLPaint's source code /works/, mine /doesn't/.
So, here it is what I have:
Controller.m
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded) name:@"shake" object:nil];
}
ShakingEnabledWindow.m
- (void)shakeEnded {
NSLog(@"Shaking ended.");
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake ) {
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
NSLog(@"Shaken!");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
My XIB has a window, which is a ShakingEnabledWindow and an object, my Controller.
I am running out of ideas here, hope someone can give me a hand. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSNotificationCenter 的文档说:
所以你的 shakeEnded 方法是错误的,因为它不带参数。它应该看起来:
Documentation of NSNotificationCenter says:
So your shakeEnded method is wrong as it takes no parameters. It should look:
在
viewDidAppear
中,成为第一响应者:并确保你可以成为第一响应者:
然后就可以实现运动检测了。
In
viewDidAppear
, become the first responder:And make sure you can be first responder:
Then you can implement the motion detection.
我认为您错误地检查了运动类型。您需要检查
event.subtype
而不是motion
:I think you are incorrectly checking the motion type. You need to check against
event.subtype
instead ofmotion
: