您的位置:首页 > 其它

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

2015-07-27 21:40 441 查看
添加AVFoundation.framework库文件





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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: