當我們用微信的時候是不是感覺搖一搖的功能很炫呢?是不是感覺實現起來比較麻煩呢?
其實,不然,這些都是蘋果已經給我們封裝好了,給我提供了非常簡單的入口了。
在UIResponder中有這些方法:
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"began");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"cancel");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"end");
}
在UIViewController的子類中都可以直接使用這些方法,因爲UIViewController也是繼承於UIResponder的。
實現中,需要讓viewController這個類本身支持搖一搖這個功能。
代碼:
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
[self becomeFirstResponder];
-(BOOL)canBecomeFirstResponder {
return YES;
}