使用UISegementControl实现简易汤姆猫程序
//
// 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实现简易汤姆猫程序的更多相关文章
- [iOS基础控件 - 3.4] 汤姆猫
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- 声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码
上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com ...
- [UI基础][不会说话的汤姆猫]
会说话的汤姆猫这个APP层级风靡一时,其UI部分就是利用了序列动画的技术, 接下来 我们用汤姆猫来演示怎么制作序列动画. [要求]: 1.学会使用序列动画的方法 2.学会分析动画播放中内存占用高的问题 ...
- iOS- 利用UIImageView自己整了个不会说话的汤姆猫
1.实现思路 先说说我实现它的主要思路,很简单,主要利用UIImageView连续动画播放,和按钮的点击事件,就可以完成了这么一个简单的不会说话的汤姆猫. 2.实现细节 2.1.加载本地字典里保存的本 ...
- 【Web网站服务器开发】apache和tomcat 阿帕奇和汤姆猫
经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...
- Web核心之tomcat汤姆猫
web相关概念 1. 软件架构 1. C/S:客户端/服务器端 2. B/S:浏览器/服务器端 2. 资源分类 1. 静态资源:所有用户访问后,得到的结果都是一样的,称为静态资源.静态资源可以直接被浏 ...
- 156 UIImageView 和 CADisplayLink 实现 Tom 汤姆猫动画效果的区别(扩展知识:分组(黄色文件夹)和文件夹引用(蓝色文件夹)区别)
(1)UIImageView 的动画操作,来自定义循环播放动画(不建议使用,内存消耗大) (2)CADisplayLink 是一个计时器,但是同 NSTimer 不同的是,CADisplayLink ...
- iOS_5_汤姆猫
终于效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...
- IOS 汤姆猫核心代码
// // MJViewController.m // 03-Tom // // Created by apple on 13-11-24. // Copyright (c) 2013年 itcast ...
随机推荐
- 使用github创建博客
本文主要介绍以下几个内容: 1.使用githbu创建自己的博客 2.将博客域名映射到自己的域名 3.如果写博客 一.使用github创建自己的博客 具体可参考https://pages.githu ...
- Phpcms v9系统类库与函数库调用方法
在分享了n多phpcms的教程后,cmsyou继续分享关于phpcms v9系统类库与函数库的调用方法. 系统类库位于系统的 /libs/functions目录下面,函数库文件名为*.func.php ...
- 6 个轻量级并且灵活的 PHP CMS 系统
Anchor CMS Just like its introduction says, Anchor has a very simple UI. The installation takes lite ...
- C51的编程规范
现在单片机的程序设计,C51已经得到广泛的推广和应用,算是单片机的主流设计程序,甚至可以说作为单片机开发人员必须要掌握的一门语言了.作为一门工具,最终的目的就是实现功能.在满足这个前提条件下,我们希望 ...
- 总结:man, info, hwclock, cal, type, which, dirname, basename, who, which, whatis, makewhatis, hash, tty
man, info, help, date, hwclock, cal, shutdown, reboot, halt, poweroff, type, cd, which, dirname, bas ...
- bootstrap栅格布局,第一次成功
代码: <div class="helper" style="background-color: #F7F7F9;height: 200px;padding-top ...
- Qt入门(4)——Qt常见控件
Qt提供了大量的内建控件及通用对话框可满足程序员的绝大部分要求.我们将对这些控件和对话框作一个大概的介绍. 1. QLabel 定义 QLabel* m_labelOrdered = newQLabe ...
- HDU2196-Computer
原题连接: http://acm.hdu.edu.cn/showproblem.php?pid=2196 思路: 好了,无敌了,经过昨晚4个钟头+今上午1个小时的奋战,这题终于被我AC了 收获的确是不 ...
- Java[2] 分布式服务架构之java远程调用技术浅析(转http://www.uml.org.cn/zjjs/201208011.asp)
转自:http://www.uml.org.cn/zjjs/201208011.asp 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如: ...
- 在Eclipse里设置格式化代码时不格式化注释
在Eclipse里设置格式化代码时不格式化注释 今天格式化代码 发现直接format会把注释也一块格式化了,有时候会把好好的注释弄的很乱.甚为头疼. 查阅之后解决办法如下: Windows -> ...