How to use Shake API in iPhone SDK 3.0?

只愿长相守 提交于 2019-12-28 01:55:36

问题


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.


回答1:


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;

Generally you just implement this:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.type == UIEventSubtypeMotionShake) {
    //Your code here
  }
}

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:.




回答2:


I posted a complete 3.0 example in this thread:

How do I detect when someone shakes an iPhone?




回答3:


Joe Hewitt recently committed some code to Three20 that utilizes the 3.0 shake event. Seems like you just need to implement some simple code within -motionBegan:withEvent: inside of your UIResponder.

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventSubtypeMotionShake) {
        ...
    }
}


来源:https://stackoverflow.com/questions/1170917/how-to-use-shake-api-in-iphone-sdk-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!