//
// 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

使用UISegementControl实现简易汤姆猫程序的更多相关文章

  1. [iOS基础控件 - 3.4] 汤姆猫

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  2. 声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码

    上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com ...

  3. [UI基础][不会说话的汤姆猫]

    会说话的汤姆猫这个APP层级风靡一时,其UI部分就是利用了序列动画的技术, 接下来 我们用汤姆猫来演示怎么制作序列动画. [要求]: 1.学会使用序列动画的方法 2.学会分析动画播放中内存占用高的问题 ...

  4. iOS- 利用UIImageView自己整了个不会说话的汤姆猫

    1.实现思路 先说说我实现它的主要思路,很简单,主要利用UIImageView连续动画播放,和按钮的点击事件,就可以完成了这么一个简单的不会说话的汤姆猫. 2.实现细节 2.1.加载本地字典里保存的本 ...

  5. 【Web网站服务器开发】apache和tomcat 阿帕奇和汤姆猫

    经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...

  6. Web核心之tomcat汤姆猫

    web相关概念 1. 软件架构 1. C/S:客户端/服务器端 2. B/S:浏览器/服务器端 2. 资源分类 1. 静态资源:所有用户访问后,得到的结果都是一样的,称为静态资源.静态资源可以直接被浏 ...

  7. 156 UIImageView 和 CADisplayLink 实现 Tom 汤姆猫动画效果的区别(扩展知识:分组(黄色文件夹)和文件夹引用(蓝色文件夹)区别)

    (1)UIImageView 的动画操作,来自定义循环播放动画(不建议使用,内存消耗大) (2)CADisplayLink 是一个计时器,但是同 NSTimer 不同的是,CADisplayLink ...

  8. iOS_5_汤姆猫

    终于效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...

  9. IOS 汤姆猫核心代码

    // // MJViewController.m // 03-Tom // // Created by apple on 13-11-24. // Copyright (c) 2013年 itcast ...

随机推荐

  1. Oracle数据库之创建表空间与用户

    Oracle数据库之创建表空间与用户 一.创建表空间 基本语法表述: CREATE TABLESPACE tablespace_name [DATAFILE datafile_spec1 [,data ...

  2. smarty练习:考试系统

    考试系统 (0607) 做一个类似于驾校考试的系统,可以选择要考试试题类型,选好后进入考试页面 使用的数据库表格:timu(题目)表,xuanxiang(选项)表,shiti(试题)表,shititi ...

  3. javascript 正则匹配手机号码

      <form class="form-horizontal" name="mobileform" style="padding:10px;&q ...

  4. C# 文件创建时间,修改时间

    System.IO.FileInfo fi = new System.IO.FileInfo(@"D:\site\EKECMS\skin\Grey\default#.html"); ...

  5. 摇滚吧HTML5!有声前端交互!(Hello, Jsonic!)

    软件工程师们摆弄1和0编写他们的乌托邦,音乐人门把玩12平均律上的音符构筑他们的伊甸园.最近,我偶然看了<蓝色骨头>这部电影,片中的男主角是位黑客,同时又兼具音乐创作的才华.在现实生活中, ...

  6. [JS]_proto_和prototype到底有啥区别

    是时候拿出我珍藏多年的这张图了: 首先,要明确几个点: 1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的 ...

  7. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  8. Linux企业级项目实践之网络爬虫(30)——通过查阅RFC文档扩充更加复杂的功能

    HTTP是一种很简单的请求.响应式协议,客户端发送一个请求.服务器返回一个响应.HTTP 1.1 版本规范由 RFC2616 定义.了解了 HTTP请求.响应消息在TCP数据流中的格式,很容易使用纯 ...

  9. 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...

  10. HDU_1016——素环问题DFS

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...