旋转的风车(声音越大转速越快)
2015-07-27 21:40 甘雨路 阅读(516) 评论(0) 编辑 收藏 举报添加AVFoundation.framework库文件
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 8 @end
1 #import "AppDelegate.h" 2 #import "RootViewController.h" 3 @interface AppDelegate () 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 12 // Override point for customization after application launch. 13 self.window.backgroundColor = [UIColor whiteColor]; 14 15 self.window.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 16 17 [self.window makeKeyAndVisible]; 18 return YES; 19 } 20 21 22 @end
1 #import <UIKit/UIKit.h> 2 #import <AVFoundation/AVFoundation.h> 3 @interface RootViewController : UIViewController 4 { 5 @private 6 AVAudioRecorder *recorder; 7 NSTimer *levelTimer; 8 double lowPass; 9 } 10 @property (weak, nonatomic) IBOutlet UIImageView *fan; 11 12 @end
1 #import "RootViewController.h" 2 3 @interface RootViewController () 4 5 @end 6 7 @implementation RootViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 [self initRecorder]; 12 } 13 14 /** 15 * 初始化AVAudioRecorder 16 */ 17 - (void)initRecorder{ 18 NSError *error; 19 NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; 20 NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:4100.0],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey,[NSNumber numberWithInt:1],AVNumberOfChannelsKey,[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil]; 21 recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; 22 if (recorder) { 23 recorder.meteringEnabled = YES; 24 [recorder record]; 25 levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(audioLevelTimerCallback:) userInfo:nil repeats:YES]; 26 }else{ 27 NSLog(@"Error:%@",[error description]); 28 } 29 } 30 31 - (void)audioLevelTimerCallback:(NSTimer *)timer{ 32 [recorder updateMeters]; 33 double peakPowerForChannel = pow(10, 0.05*[recorder peakPowerForChannel:0]); 34 lowPass = 0.05 * peakPowerForChannel + (1 - 0.05) * lowPass; 35 [self rotateFanToAngle:(lowPass - 0.05)/(1 - 0.05)]; 36 37 } 38 39 /** 40 * 旋转风扇 41 */ 42 - (void)rotateFanToAngle:(double)angle{ 43 [UIView transitionWithView:self.fan duration:angle*1.2 options:UIViewAnimationCurveEaseOut animations:^{ 44 self.fan.transform = CGAffineTransformRotate(self.fan.transform, angle/3); 45 } completion:nil]; 46 } 47 48 - (void)didReceiveMemoryWarning { 49 [super didReceiveMemoryWarning]; 50 // Dispose of any resources that can be recreated. 51 } 52 53 54 55 @end
在xib文件中拖入UIImageView,然后添加相应的图片