【发布时间】:2010-11-13 08:14:45
【问题描述】:
Apple 在 iPhone SDK 3.0 中宣布了 Shake API。我找不到有关此新功能的任何信息。
谁知道如何使用它?任何示例,链接都会很好。
【问题讨论】:
标签: iphone accelerometer shake
Apple 在 iPhone SDK 3.0 中宣布了 Shake API。我找不到有关此新功能的任何信息。
谁知道如何使用它?任何示例,链接都会很好。
【问题讨论】:
标签: iphone accelerometer shake
Joe Hewitt 最近committed 向Three20 发送了一些代码,该代码利用了 3.0 抖动事件。似乎您只需要在您的UIResponder 内部的-motionBegan:withEvent: 内实现一些简单的代码。
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
...
}
}
【讨论】:
您要查找的 API 在UIResponder:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
通常你只需实现这个:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
//Your code here
}
}
在您的 UIViewController 子类中(UIViewController 是 UIResponder 的子类)。此外,您希望在 motionEnded:withEvent: 中处理它,而不是在 motionBegan:withEvent: 中处理。 motionBegan:withEvent: 当手机怀疑正在发生晃动时调用,但操作系统可以确定用户故意晃动和偶然晃动(如走上楼梯)之间的区别。如果操作系统在调用 motionBegan:withEvent: 后确定它不是真正的抖动,它将调用 motionCancelled: 而不是 motionEnded:withEvent:。
【讨论】:
[self becomeFirstResponder]; 对吗?
我在这个帖子中发布了一个完整的 3.0 示例:
【讨论】: