国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[原創]控制iphone震動強度和時間的privateapi

2019-11-14 20:42:57
字體:
來源:轉載
供稿:網友

主要調用AudioServicesPlaySystemSoundWithVibration這個PRivate api,傳入一個含有時間和強度的dictionary即可。
注意要引入AudioToolbox.framework。在iOS6,iOS7上測試通過。

private api有風險,注意不要在上架app中使用。

 1 NSMutableDictionary* dict = [NSMutableDictionary dictionary]; 2 NSMutableArray* arr = [NSMutableArray array ]; 3  4 [arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms 5 [arr addObject:[NSNumber numberWithInt:2000]]; 6  7  8 [dict setObject:arr forKey:@"VibePattern"]; 9 [dict setObject:[NSNumber numberWithFloat:0.3] forKey:@"Intensity"];10 11 AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff);

 




寫了一個封裝demo
.h

 1 // 2 // YANGZShaking.h 3 // shaking 4 // 5 // Created by mmm on 14-1-11. 6 // Copyright (c) 2014年 yangz. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface YANGZVibePattern : NSObject12 + (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe;13 @property (nonatomic) bool isV;14 @property (nonatomic) float intensity;15 @property (nonatomic) NSUInteger time;16 17 @end18 19 @interface YANGZShaking : NSObject20 21 22 - (YANGZShaking *)initWithPattern:(NSArray *)patterns;23 - (YANGZShaking *)initWithFile:(NSString *)file;24 - (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause;25 - (void)play;26 - (void)stop;27 28 @end

 

.m

  1 //  2 //  YANGZShaking.m  3 //  shaking  4 //  5 //  Created by mmm on 14-1-11.  6 //  Copyright (c) 2014年 yangz. All rights reserved.  7 //  8   9  10 #import "YANGZShaking.h" 11 #import <AudioToolbox/AudioToolbox.h> 12  13  14 @implementation YANGZVibePattern 15 + (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe{ 16     YANGZVibePattern *pattern = [YANGZVibePattern new]; 17     pattern.isV = isVibe; 18     pattern.intensity = intensity; 19     pattern.time = time; 20     return pattern; 21 }; 22 @end 23  24  25 @interface YANGZShaking(){ 26     NSArray *_patterns; 27     NSMutableDictionary *_buff; 28     bool _isRepeat; 29 } 30 @end 31  32  33 @implementation YANGZShaking 34  35  36 - (YANGZShaking *)initWithPattern:(NSArray *)patterns{ 37     if ([super init]) { 38         _patterns = [[NSMutableArray alloc] initWithArray:patterns copyItems:YES]; 39         _buff=nil; 40         _isRepeat=NO; 41     } 42 return self; 43 } 44  45  46 - (YANGZShaking *)initWithFile:(NSString *)file{ 47     if ([super init]) { 48         _patterns = [NSMutableArray arrayWithContentsOfFile:file]; 49         _buff=nil; 50         _isRepeat=NO; 51     } 52 return self; 53 } 54 - (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause{ 55     if ([super init]) { 56         YANGZVibePattern *vibePattern = [YANGZVibePattern PatternWithIntensity:0.3 time:vibe isVibe:YES]; 57         YANGZVibePattern *pausePattern = [YANGZVibePattern PatternWithIntensity:0.9 time:pause isVibe:NO]; 58  59         _patterns = @[vibePattern,pausePattern]; 60         _buff=nil; 61         _isRepeat=YES; 62     } 63 return self; 64  65  66 } 67 - (void)play{ 68     [NSThread detachNewThreadSelector:@selector(loop) toTarget:self withObject:nil]; 69 } 70 - (void)stop{ 71  72 } 73  74  75 - (void)loop{ 76     NSUInteger i=0; 77     while (YES) { 78         NSUInteger pause = 0; 79         YANGZVibePattern *pattern = _patterns; 80         if (!pattern.isV) { 81         } 82         else{ 83             [self prepareBuffWithPattern:pattern]; 84             [self performSelectorOnMainThread:@selector(systemPlay) withObject:nil waitUntilDone:NO]; 85         } 86         [NSThread sleepForTimeInterval:pattern.time*1.0f/1000]; 87         i++; 88         if (i==[_patterns count] && !_isRepeat) { 89             break; 90         } 91         i=i%[_patterns count]; 92     } 93 } 94 - (void)prepareBuffWithPattern:(YANGZVibePattern *)pattern{ 95     NSMutableDictionary* dict = [NSMutableDictionary dictionary]; 96     NSMutableArray* arr = [NSMutableArray array ]; 97  98     [arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms 99     [arr addObject:[NSNumber numberWithInt:pattern.time]];100 101 102 103     [dict setObject:arr forKey:@"VibePattern"];104     [dict setObject:[NSNumber numberWithFloat:pattern.intensity] forKey:@"Intensity"];105     _buff = dict;106 }107 - (void)systemPlay{108     NSLog(@"system play @%");109 110     AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff);111 }112 @end

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邳州市| 绥棱县| 临桂县| 景德镇市| 仙桃市| 栖霞市| 共和县| 霸州市| 青岛市| 罗江县| 龙陵县| 临汾市| 华亭县| 车险| 南召县| 泾阳县| 云梦县| 富锦市| 内黄县| 绥滨县| 和田市| 瑞安市| 永川市| 涟水县| 资兴市| 务川| 新邵县| 延长县| 尼木县| 凤山市| 额敏县| 本溪| 泽州县| 苗栗县| 延川县| 伊春市| 临海市| 双桥区| 揭西县| 达日县| 两当县|