IOS--简单的汤姆猫代码
来源:互联网 发布:淘宝开放平台如何赚钱 编辑:程序博客网 时间:2024/04/28 14:56
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
// 汤姆猫图像视图
@property (weak, nonatomic) IBOutlet UIImageView *tomcatImageView;
// 动画操作
- (IBAction)animationAction:(UIButton *)sender;
@end
-------------------------------------------------------------------------------------------------
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
typedef enum
{
kTomCatFart = 0, // 放屁
kTomCatCymbal, // 打叉
kTomcatDrink, // 喝牛奶
kTomCatEat, // 吃鸟
kTomCatPie, // 拍饼
kTomCatScratch, // 玻璃
kTomCatKnockout, // 打脸
kTomCatStomach, // 肚皮
kTomCatFootRight, // 右脚
kTomCatFootLeft, // 左脚
kTomCatAngryTail, // 尾巴
} kTomCatAnimationType;
@interface ViewController ()
{
// 汤姆猫数据字典
NSMutableDictionary *_tomcatDict;
// 音效的数据字典
NSMutableDictionary *_soundDict;
}
@end
@implementation ViewController
/**
用数据字典来实现音效的管理
*/
- (SystemSoundID)loadSoundId:(NSString *)soundFile
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:soundFile ofType:nil]];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundId);
return soundId;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 数据初始化工作,加载数据字典成员变量
// 1. 需要指定路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Tomcat" ofType:@"plist"];
// 2. 加载数据字典
_tomcatDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@", _tomcatDict);
// 3. 初始化音效字典
_soundDict = [NSMutableDictionary dictionary];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
// 汤姆猫动画操作
- (IBAction)animationAction:(UIButton *)sender
{
// 如果汤姆猫正在动画中,不允许中断动画
if ([_tomcatImageView isAnimating]) {
return;
}
// 1. 判断按钮的tag,根据不同的tag加载不同的序列帧图像数组
// 2. 设置汤姆猫的图像,开始动画
// 要让代码的可读性更好,可以考虑使用枚举来替代tag的数字
// 引入数据字典,会简化我们的操作!
NSDictionary *dict;
switch (sender.tag) {
case kTomCatAngryTail:
dict = _tomcatDict[@"angry-tail"];
break;
case kTomCatFart:
dict = _tomcatDict[@"fart"];
break;
case kTomCatCymbal:
dict = _tomcatDict[@"cymbal"];
break;
case kTomcatDrink:
dict = _tomcatDict[@"drink"];
break;
case kTomCatEat:
dict = _tomcatDict[@"eat"];
break;
case kTomCatPie:
dict = _tomcatDict[@"pie"];
break;
case kTomCatScratch:
dict = _tomcatDict[@"scratch"];
break;
default:
break;
}
// 根据选中的数据字典,初始化序列帧图像
NSMutableArray *imageList = [NSMutableArray array];
for (NSInteger i = 0; i < [dict[@"frames"]integerValue]; i++) {
NSString *imageFile = [NSString stringWithFormat:dict[@"imageFormat"], i];
UIImage *image = [UIImage imageNamed:imageFile];
[imageList addObject:image];
}
//--------------------------------------------------
// 音频处理部分代码
// 1) 从汤姆猫的数据字典中,首先取出声音文件的数组
NSArray *array = dict[@"soundFiles"];
// 2) 判断数组中是否有数据,如果有数据做进一步处理
SystemSoundID soundId = 0;
if (array.count > 0) {
// 3) 我们根据数组中得文件名,判断音频字典中是否有对应的记录,如果没有,建立新的音频数据字典
for (NSString *fileName in array) {
SystemSoundID playSoundId = [_soundDict[fileName]unsignedLongValue];
// 如果在字典中没有定义音频代号,初始化音频Id,并且加入字典
if (playSoundId <= 0) {
playSoundId = [self loadSoundId:fileName];
// 将playSoundId加入到数据字典,向字典中增加数值,不是用add
// 向NSDict NSArray中添加数值需要“包装”
// @() 会把一个NSInteger的数字,变成NSNumber的对象
[_soundDict setValue:@(playSoundId) forKey:fileName];
}
}
NSLog(@"============================");
NSLog(@"%@", _soundDict);
// 每一个动画的声音可以是多个,为了保证游戏的可玩度,可以采用随机数的方式播放音效
NSInteger seed = arc4random_uniform(array.count);
NSString *fileName = array[seed];
soundId = [_soundDict[fileName]unsignedLongValue];
}
// 设置图像的动画属性
[_tomcatImageView setAnimationImages:imageList];
[_tomcatImageView setAnimationDuration:[dict[@"frames"]integerValue] / 10.0];
[_tomcatImageView setAnimationRepeatCount:1];
[_tomcatImageView startAnimating];
// 播放声音
if (soundId > 0) {
AudioServicesPlaySystemSound(soundId);
}
}
@end
- IOS--简单的汤姆猫代码
- IOS 汤姆猫核心代码
- iOS学习之路-简单汤姆猫
- 汤姆猫实现代码
- iOS 自制汤姆猫
- iOS -UI汤姆猫实现(UIImageView)自己写的汤姆猫,所学知识有限,代码还有待改进 ==
- IOS汤姆猫小练习
- ios-day02-05(汤姆猫)
- 汤姆猫iOS案例总结
- iOS-汤姆猫项目总结
- 【成长记录】iOS小程序-不会说话的汤姆猫
- IOS基础UI之(四)汤姆猫的实现
- **原创**零基础学ios**UI课程03***汤姆猫动画效果实现代码
- [iOS] 经典应用-汤姆猫
- ios-UI-汤姆猫德游戏实现
- iOS开发-------汤姆猫实例(UIImageView)
- 汤姆猫
- 汤姆猫
- U3D插件 Build Report Tool 自述文件
- SPOJ VLATTICE - Visible Lattice Points 莫比乌斯反演
- 分支-19. 阶梯电价(15)
- NSAttributeString和NSMutableAttributeString
- HTML常用标签的属性(学习笔记,慢慢更新中...)
- IOS--简单的汤姆猫代码
- java常见面试题
- 编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
- hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp
- win7/8 修改硬盘图标的方法
- codeigniter 连接oracle 执行insert语句时报错
- 蓝桥杯 ALGO-1 区间k大数查询(数组、排序)
- 安卓下载更新
- LeetCode 045 Jump Game II