iOS Development-Advanced ui-animations

Source: Internet
Author: User

One, implicit animation
* Each UIView internal is associated with a calayer, we can call this layer as rootlayer (root layer), this automatically created Calayer there is no implicit animation.
* All non-root layers, which are manually created Calayer objects, have implicit animations.
* What is an implicit animation?
When you modify some properties of a non-root layer, some animation effects are automatically generated by default.
These properties are called Animatable properties (animated property)

* Several common animatable Properties are listed:
Bounds: Sets the width height of the calayer. Modifying this property will result in a scaled animation
BackgroundColor: Sets the background color of the calayer. Modifying this property will result in a gradient animation of the background color
Position: Sets the location of the Calayer. Modifying this property will result in a panning animation

Close an implicit animation
[Catransaction begin];
[Catransaction Setdisableactions:yes];
[Catransaction commit];

Second, custom layer (not required to master, know can)
1. Create a new class Njlayer, overriding drawincontext in its implementation file: method
Draw a graphic on the layer in this method
Note the Drawincontext in Calayer: method does not automatically call
Can only be called by itself through the Setneeddisplay method
-(void) Drawincontext: (cgcontextref) CTX
{

1. Drawing graphics
Cgcontextaddellipseinrect (Ctx,cgrectmake (0,0,100,100));

[[Uicolor Redcolor] set];//Note that classes in the Uikit framework cannot be used

Cgcontextsetrgbfillcolor (ctx,1,0,0,1);
2. Rendering graphics
Cgcontextfillpath (CTX);
}

2. In the Controller implementation file
1. Create a custom layer
Njlayer *mylayer = [Njlayer layer];

Mylayer.bounds = CGRectMake (0,0,100,100);
Mylayer.anchorpoint = Cgpointzero;
Mylayer.backgroundcolor = [Uicolor Greencolor]. Cgcolor;

1.1. Manually call the Setneeddisplay method in Calayer to draw the graphic
[Mylayer Setneeddisplay];
2. Add a custom layer to the layer on the controller's view
[Self.view.layer Addsublayer:mylayer];

Second method of customizing layer (using Proxy, lazy method)
1. In the viewdidload of the controller implementation file
1. Create a custom layer
Calayer *mylayer = [Calayer layer];

Mylayer.bounds = CGRectMake (0,0,100,100);
Mylayer.anchorpoint = Cgpointzero;
Mylayer.backgroundcolor = [Uicolor Greencolor]. Cgcolor;

Mylayer.delegate = self; (no protocol compliance)

2. Add a custom layer to the layer on the controller's view
[Self.view.layer Addsublayer:mylayer];

2.//Custom Layer by proxy
-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX
{
1. Drawing graphics
Cgcontextaddellipseinrect (Ctx,cgrectmake (0,0,100,100));
Cgcontextsetrgbfillcolor (ctx,1,0,0,1);
2. Rendering graphics
Cgcontextfillpath (CTX);
}

3. In the Viewdidload
1. Create a custom layer
Calayer *mylayer = [Calayer layer];

Mylayer.bounds = CGRectMake (0,0,100,100);
Mylayer.anchorpoint = Cgpointzero;
Mylayer.backgroundcolor = [Uicolor Greencolor]. Cgcolor;

Mylayer.delegate = self; (no protocol compliance)

1.1. Manually call the Setneeddisplay method in Calayer to draw the graphic
[Mylayer Setneeddisplay];

2. Add a custom layer to the layer on the controller's view
[Self.view.layer Addsublayer:mylayer];

Three, core animation-Basic animation
Core animations (Introduction)
*core Animation, Chinese translation is the core animation, it is a very powerful set of animation processing API, using it can make very beautiful animation effect, and often do more with less. In other words, very powerful functionality can be achieved with a small amount of code.
*core animation can be used on Mac OS X and iOS platforms
*core animation animations are performed in the background and do not block the main thread
* It is important to note that Core animation is directly acting on calayer, not uiview.

Steps for using Core animation
1. Initialize a Caanimation object and set some animation-related properties
2. Add the Caanimation object to the Calayer by calling Calayer's Addanimation:forkey: method to start the animation.
3. You can stop the animation in Calayer by calling Calayer's Removeanimation:forkey: Method

Caanimation Inheritance Structure

Practical Use (MOBILE)
1. In the class extension of the controller implementation file
@property (Nonatomic,strong) Calayer *mylayer;

2. In the viewdidload of the controller implementation file
{
1. Create a layer
Calayer *mylayer = [Calayer layer];
Mylayer.bounds = CGRectMake (0,0,100,100);

Mylayer.anchorpoint = Cgpointzero;
mylayer.position= Cgpointmake (100,100);
Mylayer.backgroundcolor = [Uicolor Greencolor]. Cgcolor;
2. Add a custom layer to the layer on the controller's view
[Self.view.layer Addsublayer:mylayer];

Self.mylayer = Mylayer;//assigns the created Mylayer to the Mylayer property of the controller
}

3. In the Touchesbegan:withevent: method
{
1. Create a core animation
Cabasicanimation *anima = [cabasicanimation animation];
1.1. Tell the system what animations to perform
Anima.keypath = @ "position";
Sets the layer from where it is animated (because the ID object needs to be passed in, so the cgpoint is wrapped as an object.) )
If this "from" is not set, the move starts from the current position
Anima.fromvalue = [Nsvalue valuewithcgpoint:cgpointmake (0,0)];

Where to (to the specified location)
Anima.tovalue = [Nsvalue valuewithcgpoint:cgpointmake (200,300)];

How much is added based on the current position
Anima.byvalue = [Nsvalue valuewithcgpoint:cgpointmake (200,300)];

1.2. Set the animation to not delete the animation after it has finished executing
Anima.removedoncompletion = NO;
1.3. Set up the latest state for saving animations
Anima.fillmode = Kcafillmodeforwards;
1.4. Set animation time
Anima.duration = 2;
2. Add the core animation to the layer
[Self.mylayer Addanimation:anima Forkey:nil];
}

Practical use (magnification)
In the Touchesbegan:withevent: method
{
1. Create a core animation
Cabasicanimation *anima = [cabasicanimation animation];
1.1. Set the animation type
Anima.keypath = @ "Bounds";

1.2. Set the animation to not delete the animation after it has finished executing
Anima.removedoncompletion = NO;
1.3. Set up the latest state for saving animations
Anima.fillmode = Kcafillmodeforwards;
1.4. Set animation time
Anima.duration = 1;

1.5. Modifying animations
Anima.tovalue = [Nsvalue valuewithcgpoint:cgrectmake (0,0,200,200)];
2. Add the core animation to the layer
[Self.mylayer Addanimation:anima Forkey:nil];
}

Practical use (rotation)
In the Touchesbegan:withevent: method
{
1. Create a core animation
Cabasicanimation *anima = [cabasicanimation animation];
1.1. Set the animation type
Anima.keypath = @ "transform";

1.2. Set the animation to not delete the animation after it has finished executing
Anima.removedoncompletion = NO;
1.3. Set up the latest state for saving animations
Anima.fillmode = Kcafillmodeforwards;
1.4. Set animation time
Anima.duration = 1;

1.5. Modifying animations
Anima.tovalue = [Nsvalue valuewithcatransform3d:catransform3dmakerotation (m_pi_4,1,1,0)];
2. Add the core animation to the layer
[Self.mylayer Addanimation:anima Forkey:nil];
}

A different approach
In the Touchesbegan:withevent: method
{
1. Create a core animation
Cabasicanimation *anima = [cabasicanimation animation];
1.1. Set the animation type
Anima.keypath = @ "transform.scale.x",//x direction enlarged 1.5 times times

1.2. Set the animation to not delete the animation after it has finished executing
Anima.removedoncompletion = NO;
1.3. Set up the latest state for saving animations
Anima.fillmode = Kcafillmodeforwards;
1.4. Set animation time
Anima.duration = 1;

1.5. Modifying animations
Anima.tovalue = @ (1.5);
2. Add the core animation to the layer
[Self.mylayer Addanimation:anima Forkey:nil];
}

Four, key frame animation

iOS Development-Advanced ui-animations

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.