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]; Initialize the ImageView property Self.imageview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "angry_00.jpg"]; _imageview.frame = [UIScreen mainscreen].bounds; [Self.view Addsubview:_imageview]; [_imageview release]; Prepare the title array Nsarray *titles = @[@ "Eat bird", @ "angry", @ "fart"]; Use the title array to initialize Uisegmentcontrol uisegmentedcontrol *segmentcontrol = [[Uisegmentedcontrol alloc] Initwithitems:titles ]; Configuration Properties//Set frame, each item divides the total width segmentcontrol.frame = CGRectMake (60, 20, 200, 30); Segmentcontrol.tintcolor = [Uicolor yellowcolor];//font, border color//segmentcontrOl.selectedsegmentindex = 0; [Self.view Addsubview:segmentcontrol]; [Segmentcontrol release]; Set the associated event to Segementcontrol [Segmentcontrol addtarget:self Action: @selector (Handlesegmentcontrol:) forControlEvents: ( uicontroleventvaluechanged)]; } #pragma mark-uisegementcontrol associated event implementation Method-(void) Handlesegmentcontrol: (Uisegmentedcontrol *) Sender {//Sender.select Edsegementindex Gets the selected segmented subscript switch (sender.selectedsegmentindex) {case 0://eats the bird [self eat]; Break Case 1://angry [self angry]; Break Case 2://farting [self fart]; Break Default:break; }} #pragma mark-bird-eating method implementation-(void) Eat {//stop playing first, and then release the last used player object [Self.player stop]; Self.player = nil; If the animation is playing, click Do not respond to if (_imageview.isanimating) {return; }//Call get animated array method animate _imageview.animationimages = [self Getanimationarraywithimagename:@ "Eat" andimagecount:40]; _imageview.animationduration = 4; _imageview.animationrepeatcount = 1;//Click play once [_imageview startanimating];//start animation//Create player object (including prepare file path, prepare player Nsur L object, initialize player object three steps) Self.player = [[Avaudioplayer alloc] Initwithcontentsofurl:[nsurl urlwithstring:[[nsbundle mainBundle ]pathforresource:@ "P_EAT.M4A" Oftype:nil]] error:nil]; [_player release]; [_player play]; } #pragma mark-The Angry method implementation-(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-the method of farting is implemented-(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]; }//provides a method for returning an animated array two parameters (picture name and number of corresponding pictures)-(Nsmutablearray *) Getanimationarraywithimagename: (NSString *) name Andimagecount :(int) Count {//Prepare picture array Nsmutablearray *imagearray = [Nsmutablearray arraywithcapacity:count]; Loops out all the pictures in a set of animations for (int i = 0; i < count; i++) {NSString *imagename = [NSString stringWithFormat: @ "%@_%02d.jpg", Name, i];//%02d: 10 bits with a placeholder of 2 or less 10Create UIImage objects with 0 UIImage *image = [UIImage imagenamed:imagename];//[Imagearray addobject:imag E]; Add photos to the array} return imagearray; }-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Using Uisegementcontrol to implement simple Tom Cat program