捕捉摇一摇的动作然后执行下面的代理方法 :
1.- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event这是开始摇一摇的时候
2.- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event这是摇一摇结束的时候
在这两个方法里面可以自定义自己想要执行的动作:
譬如/**
* 摇一摇结束后调用
*
* @param motion motion
* @param event 事件
*/
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake) {
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"changeView"]) {
currentCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"changeView"];
}
if (currentCount ==4) {
randCount = 0;
currentCount = 0;
}else{
randCount = currentCount +1;
}
NSString *str = [NSString stringWithFormat:@"%ld",(long)randCount];
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeView" object:nil userInfo:@{@"CV":str}];
}
}