Core animation2-cabasicanimation

Source: Internet
Author: User

Cabasicanimation is a subclass of capropertyanimation that can be used to achieve some basic animation effects that allow one of Calayer's properties to fade from one value to another. Here are a few simple animations with cabasicanimation.

* First initialize a uiview to the controller's view and then animate on this uiview layer, the self is the controller

1 _myview = [[UIView alloc] init];2 _myview.layer.position = cgpointmake (+); 3 _myview.layer.bounds = CGRectMake (0, 0, 4 _myview.backgroundcolor = [Uicolor bluecolor];5 [Self.view addsubview:_myview];6 [_myview release];
one, flat mobile painting

There are several ways to implement panning animations, which are listed here in 2 ways.

1. Method 1
1//indicates that the animated object is to animate the position property of Calayer 2 cabasicanimation *anim = [cabasicanimation animationwithkeypath:@ "position"]; 3//Animation lasts 1.5s 4 anim.duration = 1.5;  5  6//Position attribute value from (50, 80) gradient to (+) 7 Anim.fromvalue = [Nsvalue valuewithcgpoint:cgpointmake (+)]; 8 Anim.tov Alue = [Nsvalue valuewithcgpoint:cgpointmake (300, 350)]; 9 10//Set the animation agent one by one anim.delegate = self;12 13//Keep the state after the animation is executed. anim.removedoncompletion = no;15 Anim.fillmode = kcafillmodef ORWARDS;16 17//Add animated object to layer [_myview.layer addanimation:anim forkey:@ "translate"];

* The KeyPath set in line 2nd is @ "position", indicating that the Calayer position property is to be modified, that is, the panning animation will be performed

* Note the 7th, 8 lines, this is not directly using Cgpoint this type of structure, but to be packaged into Nsvalue objects before use. These 2 lines of code represent Calayer moving from position (50, 80) to position (300, 350)

* If the tovalue of line 8th is replaced with Byvalue, the calayer moves from position (50, 80) to the right 300, move down 350, i.e. move to position (350, 430)

* By default, when the animation finishes, the animation is automatically removed from the Calayer and the Calayer is returned to its original state. In order to maintain the state of the animation after the execution, you can add the 14th, 15 lines of code

* The @ "translate" after line 18th is the name of the animated object, and you can call Calayer's Removeanimationforkey later: Method stops the animation according to the animated name

* Line 11th is to set the animation agent, you can listen to the animation of the execution process, where the controller is set as the agent. The methods that the agent needs to implement are:

1 #pragma mark animation starts 2-(void) Animationdidstart: (caanimation *) anim {3     NSLog (@ "animation started"), 4} 5  6 #pragma mark animation ends 7-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) Flag {8     //view the position value after the animation is finished 9     NSString * String = Nsstringfromcgpoint (_myview.layer.position);     NSLog (@ "animation is over, position:%@", string); 11}

Printing results are:

1 2013-04-14 23:44:26.197 caanimation[5995:c07] Animation started 2 2013-04-14 23:44:27.697 caanimation[5995:c07] The animation is over, position:{ 100, 100}

From the 2nd line of print information can be seen, in fact, after the completion of the animation, and did not really change the value of the Calayer position property!

2. Method 2
1 cabasicanimation *anim = [cabasicanimation animationwithkeypath:@ "transform"];2 anim.duration = 1;3 4 CATransform3D for m = catransform3dmaketranslation (0), 5 anim.tovalue = [Nsvalue valuewithcatransform3d:form];6 7 [_myView.layer A Ddanimation:anim Forkey:nil];

Panning animation is achieved by Calayer's Transform property, where the layer is shifted from its initial position to the (350, 350) position

Second, zoom animation

There are several ways to implement scaling animations, listed here in 2.

1. Method 1
1 cabasicanimation *anim = [cabasicanimation animationwithkeypath:@ "Bounds"];2 anim.duration = 2;3 4 Anim.toValue = [NSVal UE valuewithcgrect:cgrectmake (0, 0, +)];5 6 [_myview.layer Addanimation:anim Forkey:nil];

Layer will change from original size (100x100) to 30x30

2. Method 2
1 cabasicanimation *anim = [cabasicanimation animationwithkeypath:@ "transform"];2 anim.duration = 1.5; Animation continuous 1.5S3 4//calayer width from 0.5 times to twice times 5//calayer height from 0.5 times times to 1.5 times times 6 anim.fromvalue = [Nsvalue valuewithcatransform3d: Catransform3dmakescale (0.5, 0.5, 1)];7 anim.tovalue  = [Nsvalue valuewithcatransform3d:catransform3dmakescale (2, 1.5, 1)];8 9 [_myview.layer Addanimation:anim Forkey:nil];

Third, rotate the animation

In fact, you can not set Fromvalue, here only set the Tovalue

 Iv. Other

* In addition to the previous use of the position, transform attributes, in fact, there are many properties Calayer can be animated, these properties are collectively known as "Animatable properties." At the beginning of the properties of thecalayer3-layer, There is a description of how to search for these properties

* Although cabasicanimation can do a lot of basic animation effect, but there is a limitation, can only let Calayer's properties from a value gradient to another value, just a gradient between 2 values

  

Core animation2-cabasicanimation

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.