Apple 在 iPhone SDK 3.0 中宣布了 Shake API。我找不到有关此新功能的任何信息。
谁知道如何使用它?任何例子,链接都会很好。
Apple 在 iPhone SDK 3.0 中宣布了 Shake API。我找不到有关此新功能的任何信息。
谁知道如何使用它?任何例子,链接都会很好。
您正在寻找的 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:。
我在这个线程中发布了一个完整的 3.0 示例:
Joe Hewitt 最近向Three20提交了一些利用 3.0 抖动事件的代码。似乎您只需要在.-motionBegan:withEvent:
UIResponder
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
...
}
}