Functional analysis
After clicking on the corresponding button, let Tom Cat show the corresponding animation
Step analysis
1. Build UI Interface
2. Tap the Monitor button
3, according to click the button to perform the corresponding animation
Knowledge Points:
1, the use of Uiimageview frame animation
2, UIImage 2 kinds of loading mode
3. Package extraction of Duplicate code
4. How to document comments
Uiimageview frame animation related properties and methods
@property (nonatomic,copy) Nsarray *animationimages;
An array of sequence frame images that need to be played (inside are UIImage objects, which show the images in order)
@property (nonatomic) Nstimeinterval animationduration;
Duration of frame animation
@property (nonatomic) Nsinteger Animationrepeatcount;
Number of execution times for frame animations (default is infinite loop)
-(void) startanimating;
Start to perform frame animation
-(void) stopanimating;
Stop performing frame animations
-(BOOL) isanimating;
Whether frame animation is being performed
2 Ways to load UIImage
Mode one: There is a cache (the memory occupied by the picture will remain in the program)
+ (UIImage ) imagenamed: (NSString ) name;
Name is the file name of the picture
Mode two: No cache (the memory occupied by the picture will be cleared after some specific operation)
+ (UIImage ) Imagewithcontentsoffile: (nsstring ) path
-(ID) Initwithcontentsoffile: (NSString *) path;
Path is the full path of the picture
Mode two for more memory optimization
Package extraction of Duplicate code:
1, when a code repeated in many places in the program, it will cause the program and smelly and long, when the structure of the code to be modified, every place where the code is to be modified, resulting in poor program extensibility
2. Therefore, to extract the duplicated code into a method, call the method where the code is needed
The idea of extracting code
1. Put the same code in one method
2. To pass in different values as method parameters
Code excerpt: (Do not drag controls, use plain code for everyone interface)
#import "HMViewController.h" @interface hmviewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*tom;@end @implementation hmviewcontroller /** Refactoring Code: 1, copy the duplicated code into the new Method 2, adjust the method as needed about the image instantiation UIImage Uiimageview imageName: The system is recommended, but the release after the image instantiation is the responsibility of the system You cannot use the ImageName method if you want to release the picture Yourself! UIImage *image = [UIImage imagenamed:imagename]; Replace the method: [UIImage imagewithcontentsoffile:<# (NSString *) #>] Note: You must use the full path when you encounter Contentsoffile!! Tip: If you put a picture in images.xcassets (storing a frequently used picture), you cannot use Imagewithcontentsoffile: a large picture temporarily used in the supporting Files */-(void) Tomanimationwithname: (NSString*) name Count: (Nsinteger) count{//Determine if the animation if([ Self. TomIsanimating])return;//Animation image Array Nsmutablearray*imagearray = [NsmutablearrayArray];intI for(i =0; i< count; i++) {NSString*imagename = [NSStringstringwithformat:@"%@_%02d.jpg", Name,i];//uiimage *image = [UIImage imagenamed:imagename]; NSString*path = [[NSBundleMainbundle] Pathforresource:imagename OfType:Nil];UIImage*image = [UIImageImagewithcontentsoffile:path]; [Imagearray Addobject:image]; }//Set animation array Self. Tom. Animationimages= Imagearray;//Repeat once Self. Tom. Animationrepeatcount=1;//Animation duration Self. Tom. Animationduration= Self. Tom. Animationimages. Count*0.075;//Start animation[ Self. TomStartanimating];//// Click to release an array after the event has ended//self.tom.animationImages = nil;[ Self. TomPerformselector:@selector(setanimationimages:) Withobject:NilAfterdelay: Self. Tom. Animationduration];}//currenttitle can remove the button's current caption text-(ibaction) Tomaction: (UIButton*) button{[ SelfTomanimationwithname:button. CurrenttitleCount:button. Tag];} - (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning{[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}@end
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ios-Tom Cat Project Summary