如何在iPhone SDK 3.0中使用Shake API? [英] How to use Shake API in iPhone SDK 3.0?
问题描述
Apple在iPhone SDK 3.0中宣布了Shake API。我找不到有关此新功能的任何信息。
Apple annonced Shake API in iPhone SDK 3.0. I can not find any information regarding this new feature.
谁知道如何使用它?任何例子,链接都会很好。
Who knows about how to use it? Any example, link will be good.
推荐答案
您要查找的API位于 UIResponder :
The APIs you are looking for are in 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是UIResponder的子类)。另外,你想在motionEnded中处理它:withEvent:,而不是motionBegan:withEvent:。 motionBegan:withEvent:当手机怀疑正在发生晃动时被调用,但操作系统可以确定用户故意晃动和偶然震动(如走上楼梯)之间的差异。如果操作系统决定在motionBegan:withEvent:被调用后它将不会真正动摇它将调用motionCancelled:而不是motionEnded:withEvent:。
in your UIViewController subclass (UIViewController is a subclass of UIResponder). Also, you want to handle it in motionEnded:withEvent:, not motionBegan:withEvent:. motionBegan:withEvent: is called when the phone suspects shaking is happening, but the OS can determine the difference between a user purposefully shaking, and incidental shaking (like walking up the stairs). If the OS decides it was not a real shake after motionBegan:withEvent: is called it will call motionCancelled: instead of motionEnded:withEvent:.
这篇关于如何在iPhone SDK 3.0中使用Shake API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!