實(shí)現(xiàn)搖一搖功能只需要讓當(dāng)前Controller本身支持搖動(dòng)耕陷,同時(shí)讓它成為第一響應(yīng)者
第一步
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
[self becomeFirstResponder];
}
第二步
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self resignFirstResponder];
}
第三步
#pragma mark - ShakeToEdit 搖動(dòng)手機(jī)之后的回調(diào)方法
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//檢測到搖動(dòng)開始
if (motion == UIEventSubtypeMotionShake)
{
// your code
NSLog(@"檢測到搖動(dòng)開始");
}
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//搖動(dòng)取消
NSLog(@"搖動(dòng)取消");
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//搖動(dòng)結(jié)束
if (event.subtype == UIEventSubtypeMotionShake)
{
// your code
NSLog(@"搖動(dòng)結(jié)束");
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振動(dòng)效果 需要#import <AudioToolbox/AudioToolbox.h>
}
}