//
// 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. 初探react

    知道这个框架有一段时间了,可是项目中没有用到,也懒得整理,最近两天比较清闲,又想起了它.好了,废话不多说了,都是干货. react是个什么框架? 为什么用react? react 的原理 react有 ...

  2. PHP+mysql统计排名第几位

    正在开发积分系统!其中有一项数据是显示用户积分排名?一下子想不到太好的办法! 最简的情况是统一某一字段的积分数据排名?比如积分字段,里面存的整数! 如何排名?或者说如何获得他在排序中的序列位次呢? s ...

  3. Linux系统下快速删除某个目录下大量文件

    不管是哪个操作系统,同一级目录存在太多的文件都是一件可怕的事情,不管是读取还是删除的时候. 一旦这种不幸的事情发生在你身上,而又不能完全把整个目录删掉怎么办呢? 你可以用 rm -f *.log 但是 ...

  4. Project: Individual Project - Word frequency program-11061160顾泽鹏

    一.预计用时: (1)明确要求:15min: (2)文件的遍历:1h: (3)Simple mode 词频统计:0.5h: (4)extend mode 词频统计:1h: (5)对单词词频排序输出:0 ...

  5. sqoop组件运行出错问题解决--com.mysql.jdbc.Driver

    sqoop list-tables --connect jdbc:mysql://192.168.11.94:3306/huochetoudalian --username xxx -password ...

  6. Altium designer PCB中过孔铺地连接的设置

    在Altium designer 6及更高版本如Altium Designer Winter 9.altium designer summer 9都会有这样的问题,在Altium DXP2004里面是 ...

  7. C++代码覆盖率工具Coverage Validator

    市面上的C++代码覆盖率工具大都收费,Coverage Validator也不例外.Coverage Validator应该少有人听过,我也是在stackoverflow里听别人介绍的.所以下载了试用 ...

  8. 借鉴DP思想: HouseRobberIII

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  9. Android手机音量的控制

    1.AudioManager audio=(AudioManager) super.getSystemService(Context.AUDIO_SERVICE);//获得手机的声音服务 //设置加音 ...

  10. java与数据结构(3)---java实现循环链表

    循环链表:将单链表中尾结点的指针由空指针改为指向头结点,就使整个单链表形成一个环,这种首尾相接的单链表称为单链表循环表,即循环链表. 循环链表与单链表最重要的区别是:尾结点的指针,不再是p->n ...