你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS--簡單的湯姆貓代碼

IOS--簡單的湯姆貓代碼

編輯:IOS開發綜合

#import

@interface ViewController : UIViewController

// 湯姆貓圖像視圖
@property (weak, nonatomic) IBOutlet UIImageView *tomcatImageView;

// 動畫操作
- (IBAction)animationAction:(UIButton *)sender;

@end

-------------------------------------------------------------------------------------------------

#import "ViewController.h"
#import

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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved