IOS-simple Tom code
# Import
@ Interface ViewController: UIViewController
// Tom Image view
@ Property (weak, nonatomic) IBOutlet UIImageView * tomcatImageView;
// Animation operation
-(IBAction) animationAction :( UIButton *) sender;
@ End
Bytes -------------------------------------------------------------------------------------------------
# Import "ViewController. h"
# Import
Typedef enum
{
KTomCatFart = 0, // fart
KTomCatCymbal, // cross
KTomcatDrink, // drink milk
KTomCatEat, // eat birds
KTomCatPie, // pie
KTomCatScratch, // glass
KTomCatKnockout, // face hitting
KTomCatStomach, // belly
KTomCatFootRight, // right foot
KTomCatFootLeft, // left foot
KTomCatAngryTail, // tail
} KTomCatAnimationType;
@ Interface ViewController ()
{
// Tom Data Dictionary
NSMutableDictionary * _ tomcatDict;
// Audio data dictionary
NSMutableDictionary * _ soundDict;
}
@ End
@ Implementation ViewController
/**
Use data dictionaries to manage sound effects
*/
-(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.
// Data initialization, loading data dictionary member variables
// 1. Specify the path
NSString * path = [[NSBundle mainBundle] pathForResource: @ "Tomcat" ofType: @ "plist"];
// 2. Load the data dictionary
_ TomcatDict = [NSMutableDictionary dictionaryWithContentsOfFile: path];
NSLog (@ "% @", _ tomcatDict );
// 3. initialize the audio dictionary
_ SoundDict = [NSMutableDictionary dictionary];
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
# Pragma mark-Actions
// Tom animation operation
-(IBAction) animationAction :( UIButton *) sender
{
// If Tom is in animation, animation interruption is not allowed.
If ([_ tomcatImageView isAnimating]) {
Return;
}
// 1. Determine the tag of the button and load different sequence frame image arrays based on different tags
// 2. Set the image of Tom to start animation.
// To make the code more readable, you can use enumeration to replace the number of the tag.
// Introducing a data dictionary will simplify our operations!
NSDictionary * dict;
Switch (sender. tag ){
Case kmeanatangrytail:
Dict = _ tomcatDict [@ "angry-tail"];
Break;
Case kmeanatfart:
Dict = _ tomcatDict [@ "fart"];
Break;
Case kmeanatcymbal:
Dict = _ tomcatDict [@ "cymbal"];
Break;
Case kmeanatdrink:
Dict = _ tomcatDict [@ "drink"];
Break;
Case kmeanateat:
Dict = _ tomcatDict [@ "eat"];
Break;
Case kmeanatpie:
Dict = _ tomcatDict [@ "pie"];
Break;
Case kmeanatscratch:
Dict = _ tomcatDict [@ "scratch"];
Break;
Default:
Break;
}
// Initialize the sequence frame image based on the selected data dictionary
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];
}
//--------------------------------------------------
// Audio processing code
// 1) First retrieve the array of sound files from the data dictionary of Tom
NSArray * array = dict [@ "soundFiles"];
// 2) determine whether there is data in the array. If there is data for further processing
SystemSoundID soundId = 0;
If (array. count> 0 ){
// 3) determine whether there is a corresponding record in the audio Dictionary Based on the file name in the array. If not, create a new audio data dictionary.
For (NSString * fileName in array ){
SystemSoundID playSoundId = [_ soundDict [fileName] unsignedLongValue];
// If no audio code is defined in the dictionary, initialize the audio Id and add it to the dictionary
If (playSoundId <= 0 ){
PlaySoundId = [self loadSoundId: fileName];
// Add playSoundId to the data dictionary to add a value to the dictionary, not add
// Add a value to NSDict NSArray to be packaged"
// @ () Converts an NSInteger number into an NSNumber object.
[_ SoundDict setValue: @ (playSoundId) forKey: fileName];
}
}
NSLog (@ "=========================== ");
NSLog (@ "% @", _ soundDict );
// The Sound Of each animation can be multiple. To ensure the playability of the game, a random number can be used to play the sound.
NSInteger seed = arc4random_uniform (array. count );
NSString * fileName = array [seed];
SoundId = [_ soundDict [fileName] unsignedLongValue];
}
// Set the animation attributes of the image
[_ TomcatImageView setAnimationImages: imageList];
[_ Effecatimageview setAnimationDuration: [dict [@ "frames"] integerValue]/10.0];
[_ Effecatimageview setAnimationRepeatCount: 1];
[_ TomcatImageView startAnimating];
// Play the sound
If (soundId> 0 ){
AudioServicesPlaySystemSound (soundId );
}
}
@ End