Today's Tom cat case effect:
(1) The stupidest and most stupid way:
--All the clicks are buttons, but there are 6 buttons with icons, some head, left and right feet, belly, tail that block is also a button, but no background text without Borders button users can not be pressed.
-The core of the frame animation here is an attribute animationimages of the Uiimageview object, which is a picture stored in an array form. Of course, there is an important method startanimating used to play the previous attribute inside the picture, the formation of animation. Again, of course, there are properties for setting the time and number of plays.
Note the point:
--we generally put the picture in the Images.xcassets, and both the PNG format and the JPG format seem to omit the suffix.
So far, almost only one code simplification method is used, that is, formatted output, using%02d to represent 00, 01......18, 19 ... these sequences, thus importing numerous pictures.
Use a 3.5-inch screen to simulate better//whether it is JPG or PNG, you can omit the picture suffix #import "ViewController.h" @interface Viewcontroller () @property when referencing a file ( Nonatomic,retain) Uiimageview *imgview1, @end @implementation viewcontroller-(void) Viewdidload {//UIImageView in the middle, Can still display the picture, the following dynamic play frame animation is also used it self.imgview1=[[uiimageview alloc]init]; Self.imgview1.frame=cgrectmake (0, 0, 320, 512); Self.imgview1.image=[uiimage imagenamed:@ "Angry_00.jpg"]; Self.imgview1.contentmode=uiviewcontentmodescaleaspectfit; [Self.view AddSubview:self.imgView1]; Define 6 visible buttons on both sides, plus a head, left and right foot, belly, tail 5 stealth buttons to provide click UIButton *btncymbal=[[uibutton alloc]init]; Btncymbal.frame=cgrectmake (5, 260, 60, 60); [Btncymbal setimage:[uiimage imagenamed:@ "Cymbal.png"] forstate:uicontrolstatenormal]; [Btncymbal addtarget:self Action: @selector (Cymbalclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btncymbal]; UIButton *btndrink=[[uibutton Alloc]init]; Btndrink.frame=cgrectmake (5, 340, 60, 60); [Btndrink SetimaGe:[uiimage imagenamed:@ "Drink.png"] forstate:uicontrolstatenormal]; [Btndrink addtarget:self Action: @selector (Drinkclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btndrink]; UIButton *btneat=[[uibutton Alloc]init]; Btneat.frame=cgrectmake (5, 420, 60, 60); [Btneat setimage:[uiimage imagenamed:@ "Eat.png"] forstate:uicontrolstatenormal]; [Btneat addtarget:self Action: @selector (Eatclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btneat]; UIButton *btnfart=[[uibutton Alloc]init]; Btnfart.frame=cgrectmake (255, 260, 60, 60); [Btnfart setimage:[uiimage imagenamed:@ "Fart.png"] forstate:uicontrolstatenormal]; [Btnfart addtarget:self Action: @selector (Fartclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnfart]; UIButton *btnpie=[[uibutton Alloc]init]; Btnpie.frame=cgrectmake (255, 340, 60, 60); [Btnpie setimage:[uiimage imagenamed:@ "Pie.png"] Forstate:uicontrolstatenormaL]; [Btnpie addtarget:self Action: @selector (Pieclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnpie]; UIButton *btnscratch=[[uibutton Alloc]init]; Btnscratch.frame=cgrectmake (255, 420, 60, 60); [Btnscratch setimage:[uiimage imagenamed:@ "Scratch.png"] forstate:uicontrolstatenormal]; [Btnscratch addtarget:self Action: @selector (Scratchclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnscratch]; UIButton *btnleftfoot=[[uibutton Alloc]init]; Btnleftfoot.frame=cgrectmake (115, 455, 40, 25); [Btnleftfoot addtarget:self Action: @selector (Leftfootclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnleftfoot]; UIButton *btnrightfoot=[[uibutton Alloc]init]; Btnrightfoot.frame=cgrectmake (160, 455, 40, 25); [Btnrightfoot addtarget:self Action: @selector (Rightfootclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnrightfoot]; UIButton *btnhead=[[uibutton Alloc]init]; Btnhead.frame=cgrectmake (80, 90, 160, 160); [Btnhead addtarget:self Action: @selector (Headclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnhead]; UIButton *btnstomach=[[uibutton Alloc]init]; Btnstomach.frame=cgrectmake (100, 330, 120, 120); [Btnstomach addtarget:self Action: @selector (Stomachclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btnstomach]; UIButton *btntail=[[uibutton Alloc]init]; Btntail.frame=cgrectmake (216, 380, 30, 80); [Btntail addtarget:self Action: @selector (Tailclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:btntail]; [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) cymbalclick{//Create a mutable array first and add the picture with a for loop, because there are too many pictures to add to the dead. Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<13; i++) {UIImage *tmpimg=[uiimage imagenamed:[nSstring stringwithformat:@ "Cymbal_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; }//Assign this image array to our previously defined Uiimageview object ImgView1 self.imgview1.animationimages=muarr1; The following is the time to set the frame animation and play times, and finally remember to start this animation, other similar to the same self.imgview1.animationrepeatcount=1; self.imgview1.animationduration=muarr1.count*0.1; [Self.imgview1 startanimating];} -(void) drinkclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<81; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "drink_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; self.imgview1.animationduration=muarr1.count*0.1; [Self.imgview1 startanimating];} -(void) eatclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<40; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "eat_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } self.imGVIEW1.ANIMATIONIMAGES=MUARR1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) fartclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<28; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "fart_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) pieclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<24; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "pie_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) scratchclick{Nsmutablearray *muarr1=[[nsmutablearray alloc]iNIT]; for (int i=0; i<56; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "scratch_%02d.jpg", I] ]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) leftfootclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<30; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "Footleft_%02d.jpg", I ]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) rightfootclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<30; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "Footright_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } Self.imgView1.animatIONIMAGES=MUARR1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) headclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<81; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "Knockout_%02d.jpg", I ]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) stomachclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<34; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "stomach_%02d.jpg", I] ]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) tailclick{Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<26; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "angry_%02d.jpg", I]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} @end
(2) Fill the hole: Animation One animation when the click of another button fails, that is, to ensure that the current animation to operate the next.
That is, in the method code of each Click event is added as follows, judge this Uiimageview object ImaView1 if playing, then return directly, do nothing, otherwise, perform an animation operation.
[Self playtomwithname:@ "stomach" number:34];
(3) Merge code: The same direct merge, different with the parameter transfer to adjust, that is, the 11-click Method is rewritten as follows:
-(void) Playtomwithname: (NSString *) name number: (int) number{if (self.imgView1.isAnimating) return; Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<number; i++) {UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "%@_%02d.jpg", Nam E,i]]; [MuArr1 addobject:tmpimg]; } self.imgview1.animationimages=muarr1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];} -(void) cymbalclick{[self playtomwithname:@ "cymbal" number:13];} -(void) drinkclick{[self playtomwithname:@ "drink" number:81];} -(void) eatclick{[self playtomwithname:@ "eat" number:40];} -(void) fartclick{[self playtomwithname:@ "fart" number:28];} -(void) pieclick{[self playtomwithname:@ "pie" number:24];} -(void) scratchclick{[self playtomwithname:@ "Scratch" number:56];} -(void) leftfootclick{[self playtomwithname:@ "footleft" number:30];} -(void) rightfootclick{[self playtomwithname:@] Footright[Number:30];} -(void) headclick{[self playtomwithname:@ "knockout" number:81];} -(void) stomachclick{[self playtomwithname:@ "stomach" number:34];} -(void) tailclick{[self playtomwithname:@ "angry" number:26];}
(4) memory management, imagenamed load the image of the drawbacks.
After we run the above code, we find that the memory usage has risen sharply after several clicks of different buttons to play the animation. The main reason is that imagenamed load the image method: it loaded after the picture is stored in memory, convenient for the next use, so the memory is constantly new images, will be very large, on the phone will appear because of memory overload and flash back phenomenon.
After a few buttons, the memory situation is as follows:
Workaround, load the picture in other ways, using the Imagewithcontentoffile method: This method does not load the picture into memory and releases it when it is exhausted.
-(void) Playtomwithname: (NSString *) name number: (int) number{ if (self.imgView1.isAnimating) return; Nsmutablearray *muarr1=[[nsmutablearray Alloc]init]; for (int i=0; i<number; i++) { nsstring *filename=[nsstring stringwithformat:@ "%@_%02d.jpg", name,i]; NSBundle *bundle1=[nsbundle Mainbundle]; NSString *path=[bundle1 pathforresource:filename Oftype:nil]; UIImage *tmpimg=[uiimage Imagewithcontentsoffile:path]; UIImage *tmpimg=[uiimage imagenamed:[nsstring stringwithformat:@ "%@_%02d.jpg", Name,i]]; [MuArr1 addobject:tmpimg]; } SELF.IMGVIEW1.ANIMATIONIMAGES=MUARR1; Self.imgview1.animationrepeatcount=1; Self.imgview1.animationduration=1; [Self.imgview1 startanimating];}
However, we found that the memory usage is still very high, which is because we clicked the button to play a set of animations after the SELF.IMGVIEW1.ANIMATIONIMAGES=MUARR1, and this muArr1 is not released still exists, So all the pictures in the animationimages are still there.
Just don't load all of them, just the set of pictures that are currently playing will be loaded in.
(5) Clear the memory again--after playing the animation, clear the memory (in fact, it can also be called cache).
The core is: to clear the cache, only need to put the self.imgview1.animationimages=nil, then there is no strong pointer to MUARR1,MUARR1 will be recycled.
However, we can not directly use the above statement, because using the above statement, you will find that the animation has not been played has been stopped, because the animation play has a time to continue, and the implementation of the above code only a very short time.
So? We need to defer execution of the above statement. How much time is delayed? It takes 1 seconds longer than the animation to play, almost.
In addition to Self.imgview1.animationimages=nil, the method can also be used for nil assignment, the statement is [Self.imgview1 Setanimationimages:nil];
Instead of delaying the execution of a method, there is just performselector...afterdelay this method, so:
Double delaynum= (double) self.imgview1.animationduration+1.0; [Self.imgview1 performselector: @selector (setanimationimages:) Withobject:nil Afterdelay:delaynum];
(6) about the picture placed in the folder, folder directly drag and drop to Xcode in the problem.
Direct picture suggestions are dragged and dropped into images.xcassets, but if it is a folder, it is not recommended to drag and drop directly to images.xcassets because it is not found when you find a file with Mainbundle. It is recommended to drag and drop into supporting files. The color of the folder from both sides is distinguished. Overall:
--The yellow color of the folder actually can not be called a folder, we can call it a group, it just group pictures, because the pictures are all unified in the Mainbudle, rather than in these "folders."
-The Blue folder is the real folder, it is a path, if you want to access the image inside it, then we need to write this folder. So it is very inconvenient for us to use Mainbundle to find files.
(7) Supplement: How to document comments
Document comments are not the same as regular annotations, and document comments are more powerful, not only for easy reading, but also for dynamic input, which is the following effect:
To achieve this effect, the format of the note is as follows, either way, preceded by the/** is */:
Write in a line/**hello, here is the document comment *///written in multiline/** HELLO, here is the document comment */
Do not confuse the */* * * * * * *.
"iOS Dev-45" Tom Cat case: Animations, imagenamed and imagewithcontentoffile memory effects, where to place picture folders, and document comments