主要调用AudioServicesPlaySystemSoundWithVibration这个PRivate api,传入一个含有时间和强度的dictionary即可。
注意要引入AudioToolbox.framework。在iOS6,iOS7上测试通过。
private api有风险,注意不要在上架app中使用。
1NSMutableDictionary* dict =[NSMutableDictionary dictionary];2NSMutableArray* arr =[NSMutableArray array ];34[arr addObject:[NSNumber numberWithBool:YES]];//vibrate for 2000ms5[arr addObject:[NSNumber numberWithInt:2000]];678[dict setObject:arr forKey:@"VibePattern"];9[dict setObject:[NSNumber numberWithFloat:0.3] forKey:@"Intensity"];1011AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff);
写了一个封装demo
.h
1//2//YANGZShaking.h3//shaking4//5//Created by mmm on 14-1-11.6//Copyright (c) 2014年 yangz. All rights reserved.7//89#import<Foundation/Foundation.h>1011@interfaceYANGZVibePattern : NSObject12+ (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe;13@property (nonatomic)boolisV;14@property (nonatomic)floatintensity;15@property (nonatomic) NSUInteger time;1617@end1819@interfaceYANGZShaking : NSObject202122- (YANGZShaking *)initWithPattern:(NSArray *)patterns;23- (YANGZShaking *)initWithFile:(NSString *)file;24- (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause;25- (void)play;26- (void)stop;2728@end
.m
1//2//YANGZShaking.m3//shaking4//5//Created by mmm on 14-1-11.6//Copyright (c) 2014年 yangz. All rights reserved.7//8910#import"YANGZShaking.h"11#import<AudioToolbox/AudioToolbox.h>121314@implementationYANGZVibePattern15+ (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe{16YANGZVibePattern *pattern = [YANGZVibePatternnew];17pattern.isV =isVibe;18pattern.intensity =intensity;19pattern.time =time;20returnpattern;21};22@end232425@interfaceYANGZShaking(){26NSArray *_patterns;27NSMutableDictionary *_buff;28bool_isRepeat;29}30@end313233@implementationYANGZShaking343536- (YANGZShaking *)initWithPattern:(NSArray *)patterns{37if([super init]) {38_patterns =[[NSMutableArray alloc] initWithArray:patterns copyItems:YES];39_buff=nil;40_isRepeat=NO;41}42returnself;43}444546- (YANGZShaking *)initWithFile:(NSString *)file{47if([super init]) {48_patterns =[NSMutableArray arrayWithContentsOfFile:file];49_buff=nil;50_isRepeat=NO;51}52returnself;53}54- (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause{55if([super init]) {56YANGZVibePattern *vibePattern = [YANGZVibePattern PatternWithIntensity:0.3time:vibe isVibe:YES];57YANGZVibePattern *pausePattern = [YANGZVibePattern PatternWithIntensity:0.9time:pause isVibe:NO];5859_patterns =@[vibePattern,pausePattern];60_buff=nil;61_isRepeat=YES;62}63returnself;646566}67- (void)play{68[NSThread detachNewThreadSelector:@selector(loop) toTarget:self withObject:nil];69}70- (void)stop{7172}737475- (void)loop{76NSUInteger i=0;77while(YES) {78NSUInteger pause =0;79YANGZVibePattern *pattern =_patterns;80if(!pattern.isV) {81}82else{83[self prepareBuffWithPattern:pattern];84[self performSelectorOnMainThread:@selector(systemPlay) withObject:nil waitUntilDone:NO];85}86[NSThread sleepForTimeInterval:pattern.time*1.0f/1000];87i++;88if(i==[_patterns count] && !_isRepeat) {89
break;90}91i=i%[_patterns count];92}93}94- (void)prepareBuffWithPattern:(YANGZVibePattern *)pattern{95NSMutableDictionary* dict =[NSMutableDictionary dictionary];96NSMutableArray* arr =[NSMutableArray array ];9798[arr addObject:[NSNumber numberWithBool:YES]];//vibrate for 2000ms99[arr addObject:[NSNumber numberWithInt:pattern.time]];100101102103[dict setObject:arr forKey:@"VibePattern"];104[dict setObject:[NSNumber numberWithFloat:pattern.intensity] forKey:@"Intensity"];105_buff =dict;106}107- (void)systemPlay{108NSLog(@"system play @%");109110AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff);111}112@end