旋转的风车(声音越大转速越快)
2015-07-27 21:40
441 查看
添加AVFoundation.framework库文件
在xib文件中拖入UIImageView,然后添加相应的图片
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; [self.window makeKeyAndVisible]; return YES; } @end
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface RootViewController : UIViewController { @private AVAudioRecorder *recorder; NSTimer *levelTimer; double lowPass; } @property (weak, nonatomic) IBOutlet UIImageView *fan; @end
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; [self initRecorder]; } /** * 初始化AVAudioRecorder */ - (void)initRecorder{ NSError *error; NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:4100.0],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey,[NSNumber numberWithInt:1],AVNumberOfChannelsKey,[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil]; recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; if (recorder) { recorder.meteringEnabled = YES; [recorder record]; levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(audioLevelTimerCallback:) userInfo:nil repeats:YES]; }else{ NSLog(@"Error:%@",[error description]); } } - (void)audioLevelTimerCallback:(NSTimer *)timer{ [recorder updateMeters]; double peakPowerForChannel = pow(10, 0.05*[recorder peakPowerForChannel:0]); lowPass = 0.05 * peakPowerForChannel + (1 - 0.05) * lowPass; [self rotateFanToAngle:(lowPass - 0.05)/(1 - 0.05)]; } /** * 旋转风扇 */ - (void)rotateFanToAngle:(double)angle{ [UIView transitionWithView:self.fan duration:angle*1.2 options:UIViewAnimationCurveEaseOut animations:^{ self.fan.transform = CGAffineTransformRotate(self.fan.transform, angle/3); } completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在xib文件中拖入UIImageView,然后添加相应的图片
相关文章推荐
- 【位运算生成枚举序列】
- Android 折线图的实现
- OpenCV中图像及子图像ROI之间的复制
- virtual empty() const中的const有的作用
- 新生儿是否应饿着
- NSRunloop的解析及autorelease的释放时机
- iOS反射机制
- bzoj1002【fjoi2007】轮状病毒
- !HDU 4282 A very hard mathematic problem-卡时间-(二分枚举)
- zoj 3890 Wumpus bfs
- 表单开发(二):获取单选按钮,多行文本框,下拉菜单,捆绑元素数据,用户注册
- iOS开发分分钟搞定C语言——函数
- 指针与二维数组
- Javascript我学之六对象工厂函数与构造函数
- 使用命令行工具运行Xcode 7 UI Tests
- Equation Again 最大化最小值
- hdoj 2147 kiki's game【博弈】
- 二叉树的遍历的应用
- Array.prototype.slice.call(arguments)
- EhCache缓存在集群环境中同步问题