我知道的搖一搖有如下2種方案:post
1、直接用系統自帶的motionBegan方法server
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)eventblog
假如程序不響應此方法,試着加入下面方法:事件
-(BOOL)canBecomeFirstResponderget
{it
return YES;io
}event
若是還不行,建議用第二種方法。class
2、motionBegan+通知的方法object
1.在Appdelegate裏寫motionBegan方法
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"shake" object:self];
}
2.在須要接收通知的頁面添加通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shakeAction) name:@"shake" object:nil];
寫在viewDidLoad裏便可。
這裏的shakeAction就是搖一搖須要調用的方法,本身修改,通知名字對應就好,可自由修改。
三.補充搖一搖的其餘方法
/** 搖一搖結束(須要在這裏處理結束後的代碼) */
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
// 不是搖一搖運動事件
if (motion != UIEventSubtypeMotionShake) return;
NSLog(@"motionEnded");
}
/** 搖一搖取消(被中斷,好比忽然來電) */
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"motionCancelled");
}