- (void)viewDidLoad {
[superviewDidLoad];
// 设置允许摇一摇功能
[UIApplicationsharedApplication].applicationSupportsShakeToEdit =YES;
// 并让自己成为第一响应者
[selfbecomeFirstResponder];
}
#pragma mark - 摇一摇相关方法
// 摇一摇开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"开始摇动");
//播放声音
SystemSoundID soundID;
NSString *path =@"/System/Library/Audio/UISounds/sms-received1.caf";
AudioServicesCreateSystemSoundID((__bridgeCFURLRef _Nonnull)([NSURLfileURLWithPath:path]), &soundID);
AudioServicesPlayAlertSound(soundID);
//播放震动
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
return;
}
// 摇一摇取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"取消摇动");
return;
}
// 摇一摇摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype ==UIEventSubtypeMotionShake) {// 判断是否是摇动结束
NSLog(@"摇动结束");
}
return;
}
//创建动画
CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
keyAnimaion.keyPath = @"transform.rotation";
keyAnimaion.values = @[@(-10 / 180.0 * M_PI),@(10 /180.0 * M_PI),@(-10/ 180.0 * M_PI)];//度数转弧度
keyAnimaion.removedOnCompletion = NO;
keyAnimaion.fillMode = kCAFillModeForwards;
keyAnimaion.duration = 0.3;
keyAnimaion.repeatCount = MAXFLOAT;
[self.iconImageView.layer addAnimation:keyAnimaion forKey:nil];