UIResponder中存在這麼一套方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
這就是執行搖一搖的方法。那麼怎麼用這些方法呢?
很簡單,你只需要讓這個Controller本身支持搖動
同時讓他成為第一相應者:
- (void)viewDidLoad
{
[superviewDidLoad];
[[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];
[self becomeFirstResponder];
}
然後去實現那幾個方法就可以了
- (void) motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent
*)event
{
//檢測到搖動
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//搖動取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//搖動結束
if(event.subtype == UIEventSubtypeMotionShake) {
//somethinghappens
}
}