zoukankan      html  css  js  c++  java
  • 旋转的风车(声音越大转速越快)

    添加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,然后添加相应的图片

                                   

  • 相关阅读:
    命名是个技术活(转)
    我想知道的是这个月哪种商品销售量最高,比上个月怎么样?销量近几个月的走势是什么?有没有未达标的?有没有超额完成的?超额完成了多少?我可不关心这个月到底售出了多少件,几点售出的,谁买的(转)
    一个demo
    oracle 创建字段自增长——两种实现方式汇总(转)
    Ruby 一些经常使用的细节
    CMap与hash_map效率对照
    Unity 4.5.2 for Mac 下载+安装+破解
    Android中View绘制流程以及invalidate()等相关方法分析
    Android Studio 100 tips and tricks
    JavaScript-2.2 document.write 输出到页面的内容
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4681315.html
Copyright © 2011-2022 走看看