"Original" iOS animation series Tutorial (i): Coreanimation animated Image Tutorial "package Meeting"

Source: Internet
Author: User

In iOS, graphics can be divided into the following layers:

The higher the upper layer, the higher the package, the simpler the animation implementation, but the lower the degree of freedom, and vice versa. This paper mainly introduces the basic animation implementation of core animation layer.

In iOS, presentation animations can be likened to showing "making movies" in life. There are three main elements of film: Actor + script + shooting, concept analogy is as follows:

Actors--->calayer, rules the film's protagonist is who script --->caanimation, rules the film How to play, how to go, how to change the shooting --->addanimation, began to execute

First, Concept introduction

What is 1.1CALayer?

Calayer is a very similar concept with UIView, also has layer,sublayer ..., also have backgroundcolor, frame and other similar properties, we can consider UIView as a special Calayer, Only UIView can respond to events. In general, layer can have two purposes, they do not conflict with each other: first, the view related properties of the settings, including the fillet, shadow, border and other parameters, more detailed parameters Click here, and the second is to achieve the animation control of the view. Therefore, the core animation animation of a view is essentially an animation manipulation of the view's. Layer. 

What is 1.2CAAnimation?

Caanimation can be divided into four types:

    • 1.CABasicAnimation
    • By setting the start point, end point, time, the animation will move along your set point. Can be seen as a special cakeyframeanimation.
    • 2.CAKeyframeAnimation
    • Keyframe as the name implies is the key point of the frame, you can set the Calayer start point, the Middle key point, the end of the frame, the time, the animation will follow the trajectory you set to move
    • 3.CAAnimationGroup
    • Group is the combination of all the animations of this layer together. PS: A layer set a lot of animation, they will be executed at the same time, how to execute sequentially I'll talk about it.
    • 4.CATransition
    • Here are some of the animations that Apple has packaged for developers

Second, hands-on work

Practice the truth, look at an example to know:

For example, we want to achieve a heartbeat-like scaling animation can do this, divided into the actor initialization, set script, film shooting three steps:

-(void) initscalelayer{//actor initialization Calayer *scalelayer = [[Calayer alloc] init]; Scalelayer.backgroundcolor = [Uicolor Bluecolor].    Cgcolor;    Scalelayer.frame = CGRectMake (50, 50);    Scalelayer.cornerradius = 10;    [Self.view.layer Addsublayer:scalelayer];        [Scalelayer release]; Set the script cabasicanimation *scaleanimation = [cabasicanimation      Animationwithkeypath:  @ "Transform.scale" ];    Scaleanimation.fromvalue = [NSNumber numberwithfloat:1.0];    Scaleanimation.tovalue = [NSNumber numberwithfloat:1.5];    scaleanimation.autoreverses = YES;    Scaleanimation.fillmode = Kcafillmodeforwards;    Scaleanimation.repeatcount = maxfloat;        Scaleanimation.duration = 0.8; [Scalelayer addanimation:scaleanimation forkey:@ "Scaleanimation"];} 
-(void) viewdidload {    [super viewdidload];    Do any additional setup after loading the view from its nib.    [Self initscalelayer];}

Please refer to the green squares in the drawings (I do not know how to record GIFs under Mac, first borrow the KK chart). Other effects can be easily achieved by gourd painting scoop. The key to achieving a different effect is the KeyPath setting in the initialization of the Cabasicanimation object. There are several different keypath in iOS that represent different effects:

In addition, we can also use groupanimation to achieve a variety of animation combinations, in the groupanimation of the various animation types are simultaneous.

1- (void) Initgrouplayer2 {3     //actor Initialization4Calayer *grouplayer =[[Calayer alloc] init];5Grouplayer.frame = CGRectMake ( -,340+ -+ Kyoffset, -, -);6Grouplayer.cornerradius =Ten;7Grouplayer.backgroundcolor =[[Uicolor Purplecolor] cgcolor];8 [Self.view.layer Addsublayer:grouplayer];9 [Grouplayer release];Ten      One     //set the script . ACabasicanimation *scaleanimation = [cabasicanimation animationwithkeypath:@"Transform.scale"]; -Scaleanimation.fromvalue = [NSNumber numberwithfloat:1.0]; -Scaleanimation.tovalue = [NSNumber numberwithfloat:1.5]; theScaleanimation.autoreverses =YES; -Scaleanimation.repeatcount =maxfloat; -Scaleanimation.duration =0.8; -      +Cabasicanimation *moveanimation = [cabasicanimation animationwithkeypath:@"position"]; -Moveanimation.fromvalue =[Nsvalue valueWithCGPoint:groupLayer.position]; +Moveanimation.tovalue = [Nsvalue valuewithcgpoint:cgpointmake ( the- the, A grouplayer.position.y)]; atMoveanimation.autoreverses =YES; -Moveanimation.repeatcount =maxfloat; -Moveanimation.duration =2; -      -Cabasicanimation *rotateanimation = [cabasicanimation animationwithkeypath:@"transform.rotation.x"]; -Rotateanimation.fromvalue = [NSNumber numberwithfloat:0.0]; inRotateanimation.tovalue = [NSNumber numberwithfloat:6.0*M_PI]; -Rotateanimation.autoreverses =YES; toRotateanimation.repeatcount =maxfloat; +Rotateanimation.duration =2; -      theCaanimationgroup *groupannimation =[Caanimationgroup animation]; *Groupannimation.duration =2; $Groupannimation.autoreverses =YES;Panax NotoginsengGroupannimation.animations =@[moveanimation, Scaleanimation, rotateanimation]; -Groupannimation.repeatcount =maxfloat; the     //The curtain +[Grouplayer addanimation:groupannimation Forkey:@"groupannimation"]; A}
-(void) viewdidload {    [super viewdidload];    Do any additional setup after loading the view from its nib.    [Self initgrouplayer];}

Complete Demo Project point caanimationdemo.zip download

=======================================================

Reprint please indicate the program small Weng @ Blog Park, mail [email protected], welcome you and I in the c/c++/objective-c/machine vision and other fields to start communication!

Original "iOS animation series Tutorial (i): Coreanimation animated Image Tutorial" package meeting "

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.