微信的搖一搖是怎么實(shí)現(xiàn)的,在 UIResponder中存在這么一套方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)even __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);
//這就是執(zhí)行搖一搖的方法。那么怎么用這些方法呢箍鼓?
//很簡(jiǎn)單,你只需要讓這個(gè)Controller本身支持搖動(dòng)
//同時(shí)讓他成為第一相應(yīng)者:
- (void)viewDidLoad
{
[superviewDidLoad];
[[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
[selfbecomeFirstResponder];
}
//然后去實(shí)現(xiàn)那幾個(gè)方法就可以了
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//檢測(cè)到搖動(dòng)
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//搖動(dòng)取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//搖動(dòng)結(jié)束
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
swift中對(duì)應(yīng)的代碼
application.applicationSupportsShakeToEdit = true
self.becomeFirstResponder()
對(duì)應(yīng)的方法
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
super.motionBegan(motion, withEvent: event)
UIApplication.sharedApplication().keyWindow?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?) {
super.motionCancelled(motion, withEvent: event)
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
super.motionEnded(motion, withEvent: event)
}