// // TomViewController.m #import "TomViewController.h" #import <AVFoundation/AVFoundation.h> @interface TomViewController () @property (nonatomic, retain) UIImageView * imageView; @property (nonatomic, retain) AVAudioPlayer *player; @end @implementation TomViewController - (void)dealloc { self.imageView = nil; self.player = nil; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // 初始化imageView属性 self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"angry_00.jpg"]]; _imageView.frame = [UIScreen mainScreen].bounds; [self.view addSubview:_imageView]; [_imageView release]; // 准备title数组 NSArray *titles = @[@"吃鸟", @"生气", @"放屁"]; // 使用title数组初始化UISegmentControl UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:titles]; // 配置属性 // 设置frame,每一个item等分总的宽度 segmentControl.frame = CGRectMake(60, 20, 200, 30); segmentControl.tintColor = [UIColor yellowColor];// 字体,边框颜色 //segmentControl.selectedSegmentIndex = 0; [self.view addSubview:segmentControl]; [segmentControl release]; // 给segementControl设置关联事件 [segmentControl addTarget:self action:@selector(handleSegmentControl:) forControlEvents:(UIControlEventValueChanged)]; } #pragma mark - UISegementControl的关联事件实现方法 - (void)handleSegmentControl:(UISegmentedControl *)sender { // sender.selectedSegementIndex 获取选中的分段下标 switch (sender.selectedSegmentIndex) { case 0:// 吃鸟 [self eat]; break; case 1: // 生气 [self angry]; break; case 2: // 放屁 [self fart]; break; default: break; } } #pragma mark - 吃鸟的方法实现 - (void)eat { // 先停止播放,再释放上一次使用的播放器对象 [self.player stop]; self.player = nil; // 如果正在播放动画,点击不响应 if (_imageView.isAnimating) { return; } // 调用获取动画数组的方法 制作动画 _imageView.animationImages = [self getAnimationArrayWithImageName:@"eat" andImageCount:40]; _imageView.animationDuration = 4; _imageView.animationRepeatCount = 1;// 点击播放一次 [_imageView startAnimating];// 开启动画 //创建播放器对象(包括 准备文件路径, 准备播放器NSURL对象,初始化播放器对象 三步) self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"p_eat.m4a" ofType:nil]] error:nil]; [_player release]; [_player play]; } #pragma mark - 生气的方法实现 - (void)angry { [self.player stop]; self.player = nil; if (_imageView.isAnimating) { return; } _imageView.animationImages = [self getAnimationArrayWithImageName:@"angry" andImageCount:26]; _imageView.animationRepeatCount = 1; _imageView.animationDuration = 2; [_imageView startAnimating]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"angry.m4a" ofType:nil]] error:nil]; [_player release]; [_player play]; } #pragma mark - 放屁的方法实现 - (void)fart { [self.player stop]; self.player = nil; if (_imageView.isAnimating) { return; } _imageView.animationImages = [self getAnimationArrayWithImageName:@"fart" andImageCount:28]; _imageView.animationDuration = 4; _imageView.animationRepeatCount = 1; [_imageView startAnimating]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"fart003_11025.m4a" ofType:nil]] error:nil]; [_player release]; [_player play]; } // 提供一个返回动画数组的方法 两个参数(图片名 和 对应图片的数量) - (NSMutableArray *)getAnimationArrayWithImageName:(NSString *)name andImageCount:(int)count { // 准备图片数组 NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:count]; // 循环取出一组动画中的全部图片 for (int i = 0; i < count; i++) { NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];// %02d:占位为2, 10以内的十位用0 UIImage *image = [UIImage imageNamed:imageName];// 创建UIImage对象 [imageArray addObject:image]; // 照片添加到数组中 } return imageArray; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-11-10 13:20:33