码迷,mamicode.com
首页 > 其他好文 > 详细

旋转的风车(声音越大转速越快)

时间:2015-07-27 22:51:14      阅读:127      评论:0      收藏: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,然后添加相应的图片

                               技术分享

旋转的风车(声音越大转速越快)

标签:

原文地址:http://www.cnblogs.com/lantu1989/p/4681315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!