导入
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>
核心代码
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
if (//判断是否登录) {
//判断摇一摇是否生效
if (![self isValildShake]) {
return;
}
//默认震动效果
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
//默认震动效果
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
}
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
if (//判断是否登录) {
//判断摇一摇是否生效
if (![self isValildShake]) {
return;
}
//设置声音及震动
[self setSoundForShake];
//do something
} else {
//设置声音及震动
[self setSoundForShake];
//do something
}
}
}
/**
判断摇一摇是否生效
*/
- (BOOL)isValildShake {
//某个界面不生效条件
if () {
return NO;
} else {
return YES;
}
}
/**
设置摇一摇声音及震动
*/
- (void)setSoundForShake {
// 设置播放音效
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle ] pathForResource:@"shake" ofType:@".mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
// 添加摇动声音
AudioServicesPlaySystemSound (soundID);
// 设置震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
具体内容自行填充,mp3文件