IOS Development Notes-Basic UI (7) Tom Cat (Uiimageview's sequence frame animation, picture loading, method refactoring, Bundle picture footage)

Source: Internet
Author: User
Tags repetition uicontrol

Use Uiimageview, UIButton to achieve a comprehensive small case---tom Cat

Memories: Uiimageview from Uiview,uiview from Uiresponder,uibutton from Uicontrol,uicontrol from UIView

See the implementation alone, code implementation is actually relatively simple, but the creative is very rare, the art requirements are very high! It's a great game!

Functional analysis

(1) After clicking on the corresponding button, let Tom Cat show the corresponding animation

Step analysis

(1) Build UI interface, but also prepare material (2) Listen button click (3) to perform the corresponding animation according to the button clicked
Description: Only fits 3.5 inch screen

material in the images.xcassets

1> Apple highly recommended (images. Xcassets only support PNG formatpictures, because PNG format picture fidelity, do not use JPG images, JPG is not true, compression ratio is too high, there is noise.

2> images are only instantiated in the way [UIImage imagenamed] , but cannot be loaded from bundles

3> at compile time, all files in Images.xcassets will be packaged as Assets.car files

If you must use the JPG format picture, then put it in the supporting files folder

sequence frame animations for Uiimageview

Class relationships

Ns_class_available_ios(2_0) @interface uiimageview: UIView

Find an attribute; animated image (plural, array)

@property (nonatomic,copy) Nsarray *animationimages;            // The array must contain uiimages. Setting hides the single image. Default is nil

There is also a time interval

@property (nonatomic) nstimeinterval animationduration;         // For one cycle of images. The default is number of images * 1/30th of a second (i.e. fps)

Double type, which is a set of pictures that plays a period of time, by default 30 graphs per second

typedef double NSTIMEINTERVAL;

There is also a repeating property, 0 for wireless repetition, and 0 for default.

@property (nonatomic) Nsinteger      animationrepeatcount;      0 means infinite (default is 0)

There are three other object methods

-(void) startanimating; -(void) stopanimating; -(BOOL) isanimating;

Note: It is not possible to animate in parallel, such as with water, and cannot be preceded simultaneously! Need to determine the method, the above mentioned Isanimating method

The code is as follows:

#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*Tom;@end@implementationViewcontroller- (void) Tomanimationofname: (NSString *) img Andcount: (int) count{//Sequence Frame animation: Let a set of pictures, and put a movie, a picture of the play, the effect of moving up//Determines whether the animation is not synchronized    if([Self.tom isanimating]) {//directly end the animation operation method, here is no return value, nil no        return; }        //at first it must be a stored picture, 81, with a mutable arrayNsmutablearray *arrayimage =[Nsmutablearray array]; //Traverse a picture     for(inti =0; I < count; i++) {        //similar to C, format control, less than two bits 0 paddedNSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", IMG, I]; UIImage*image =[UIImage Imagenamed:name]; //Add to Array[Arrayimage Addobject:image]; }    //then start the animation//Put the picture in the Animationimages, accept the array parametersSelf.tom.animationImages =Arrayimage; //set the time interval, 81 graphs, the picture is a little longer than the time, otherwise shortSelf.tom.animationDuration = Arrayimage.count *0.074; //set number of repetitionsSelf.tom.animationRepeatCount =1; //Start Animation[Self.tom startanimating]; //End Animation}-(ibaction) head{[self tomanimationofname:@"Knockout"Andcount:Bayi];}-(ibaction) drink{[self tomanimationofname:@"Drink"Andcount:Bayi];}

However, Africa consumes memory while the program is running!

Consider the UIImage imagenamed problem (Detailed: UIImage the difference between Imagewithcontentsoffile:path and imagenamed )

After the picture is used, it will not be released directly, the specific release time determined by the system, which is not good, unreasonable. We need to change it manually. For example, if the same picture, a program is always repeated use each time to re-load from the disk, will degrade performance. (programmers are looking for balance, good relative performance and good execution time.) Can't be extreme. These are simulators that cannot be measured, because the memory used by the computer is not easily exhausted. Generally speaking:imagenmaed is suitable for small images, commonly used image processing. To load. You can also instantiate an image (full path) using [UIImage Imagewithcontentsoffile:path] .

Because the former has a cache (the memory occupied by the picture will remain in the program)

+ (UIImage *) imagenamed: (NSString *) name;The latter, 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
        NSString *name = [NSString stringWithFormat:@ "%@_%02d.jpg", IMG, i];         // UIImage *image = [UIImage imagenamed:name];         // add to Array        NSString *file = [[nsbundle Mainbundle] Pathforresource:name Oftype:nil];         *image = [UIImage imagewithcontentsoffile:file];        [Arrayimage Addobject:image];

In this case, there is no big change, then should be in the animation call, do a manual animation array of the contents of the empty! Here also note, to my condolences delay, otherwise the animation just start, emptied! Attention!

    // end animation, should wait and then empty    [Self.tom performselector: @selector (setanimationimages:) Withobject:nil afterDelay:self.tom.animationDuration];

Strategies for method refactoring in code

1> Copy the code with commonality to a new method

2> increase the parameters of the method according to the different invocation conditions

Hint: Do not worry about refactoring when writing a program, sometimes write the code first, it is easier to see how to reconstruct the better! When a code is repeated in many places of the program, it will cause the program and smelly and long, when the structure of the code to be modified, each place where the code is modified, resulting in poor program extensibility, so to extract the duplicated code into a method, where the code is required to call the method

the idea of extracting code:Put the same code in a method, passing in different values as method parameters

the picture material in the Bundle

When dragging footage into a project, you typically select

1> Destination: tick

2> Folders:

Select the first item: Yellow folder

Xcode in the folder,bundle all the material is in the same folder, the development efficiency is very high, therefore, can not appear the file name of the case, but the art is uncomfortable.

Characteristics:

You can use [NSBundle Mainbundle] directly as a resource path, high efficiency!

You can load an image using [UIImage imagenamed:]

Select the second item: Blue folder

Xcode sub-folders,bundles in the same folder, so you can have the file name of the case

Characteristics:

Need to be on the basis of [NSBundle Mainbundle] to splice The actual path, the efficiency is poor!

Cannot load image using [UIImage imagenamed:]

File Management

[Nsfilemanager Defaultmanager]

Common methods

1> determine if a file exists

-(BOOL) Fileexistsatpath: (NSString *) path;

2> copying files from the source path to the destination path

-(BOOL) Copyitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) error;

3> Deleting files

-(BOOL) removeItemAtPath: (N

Sequence frame animation development approximate steps:

// 1. Set an array of images [Self.tom setanimationimages:xxx]; // 2. Set the length of the animation, the default to play 30 pictures per second [Self.tom setanimationduration:xxx]; // 3. Set the animation repetition number, default is 0, infinite loop [Self.tom setanimationrepeatcount:xxx]; // 4. Start the animation [Self.tom startanimating]; // 5. After the animation is finished, empty the animated array [self.tom performselector: @selector (setanimationimages:) withobject:nil AfterDelay:self.tom.animationDuration];

IOS Development Notes-Basic UI (7) Tom Cat (Uiimageview animation, picture loading, method refactoring, Bundle picture footage)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.