AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (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 makeKeyAndVisible];
[_window release];
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = rootVC;
[rootVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
// 敲nsem
typedef NS_ENUM(NSUInteger, NSBackGroundColor) {
NSColorRed,
NSColorYellow,
NSColorBlue,
};
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface RootViewController ()
@property(nonatomic, retain)AVAudioPlayer *player;
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation RootViewController
- (void)dealloc
{
[_imageView release];
[_player release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
#if 0
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
[self.view addSubview:stepper];
[stepper release];
stepper.stepValue = 3; // default is 1
stepper.value = 1;
stepper.minimumValue = 0;
stepper.maximumValue = 100;
[stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventTouchUpInside];
// 先获取播放的音频文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"小城故事" ofType:@"mp3"];
// 创建player
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.player prepareToPlay];
// 播放
// [self.player play];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
button.frame = CGRectMake(100, 300, 100, 50);
[button setTitle:@"开始" forState:UIControlStateNormal];
button.layer.cornerRadius = 10;
button.layer.borderWidth = 1;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
#endif
#if 0
// 用UIImageView播放gif图
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.view addSubview:self.imageView];
[self.imageView release];
self.imageView.layer.borderWidth = 1;
self.imageView.layer.borderColor = [UIColor purpleColor].CGColor;
// 新建一个可变数组
NSMutableArray *picArray = [NSMutableArray array];
for (NSInteger i = 0; i < 40; i++) {
// NSLog(@"%02ld", i);
// 拼接图片名
NSString *imageName = [NSString stringWithFormat:@"eat_%02ld.jpg", i];
// NSLog(@"%@", imageName);
UIImage *image = [UIImage imageNamed:imageName];
// 把图片放数组里
[picArray addObject:image];
}
// 需要指定播放的数组
self.imageView.animationImages = picArray;
// 动画执行时间
self.imageView.animationDuration = 3;
// 播放次数
self.imageView.animationRepeatCount = NSIntegerMax;
[self.imageView startAnimating];
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 330, 200, 10)];
[self.view addSubview:slider];
[slider release];
slider.layer.borderWidth = 1;
slider.layer.cornerRadius = 15;
[slider sizeToFit];
[slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];
// 设置最大值和最小值
slider.minimumValue = 0;
slider.maximumValue = 3;
#endif
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"红", @"黄", @"蓝"]];
seg.frame = CGRectMake(100, 100, 200, 50);
[self.view addSubview:seg];
[seg release];
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)segAction:(UISegmentedControl *)seg {
NSLog(@"%ld", seg.selectedSegmentIndex);
switch (seg.selectedSegmentIndex) {
case NSColorRed:
self.view.backgroundColor = [UIColor redColor];break;
case NSColorYellow:
self.view.backgroundColor = [UIColor yellowColor];break;
case NSColorBlue:
self.view.backgroundColor = [UIColor blueColor];
break;
default:
break;
}
}
- (void)sliderClick:(UISlider *)slider {
NSLog(@"%g", slider.value);
self.imageView.animationDuration = slider.value;
[self.imageView startAnimating];
}
- (void)stepperAction:(UIStepper *)stepper {
NSLog(@"%g", stepper.value);
self.player.volume = stepper.value;
}
- (void)click:(UIButton *)button {
if ([button.currentTitle isEqual:@"开始"]) {
[button setTitle:@"暂停" forState:UIControlStateNormal];
[self.player play];
} else {
[button setTitle:@"开始" forState:UIControlStateNormal];
[self.player pause];
}
}