iOS development--Several methods of animation programming

Source: Internet
Author: User

Several methods of animation programming

There are five types of animations in iOS: Uiview<block>,caanimation<cabasicanimation,catransition,cakeyframeanimation> Nstimer

Here I summed up the five methods, in fact, iOS development animation programming will change here, so as long as the understanding of these animation programming is not difficult.



One: UIView animation

General Way

[UIView beginanimations:@ "ddd" context:nil];//set animation
[UIView commitanimations]; Submit Animation
These two are necessary and then add the animated code in the middle of the two sentences
[UIView beginanimations:@ "DDD" context:nil];//set animated DDD to animated name
[UIView setanimationduration:3];//defines the duration of the animation
[UIView Setanimationcurve:uiviewanimationcurveeaseinout]; Setanimationcurve to define animation acceleration or deceleration mode
[UIView Setanimationtransition:uiviewanimationtransitioncurldown ForView:self.window Cache:yes];
Sets the style of the animation Forview for which view implements this animation effect
[UIView Setanimationdelay:3]; Set how long animation delay is performed
[UIView setanimationdelegate:self]; Animating the agent implementation before and after the animation method is set before Commitanimation
[UIView setanimationdidstopselector: @selector (stop)];//sets the method to be executed after the animation finishes
[UIView Setanimationwillstartselector: @selector (Star)];//set the way the animation will start executing
[UIView commitanimations]; Submit Animation
typedef enum {
Uiviewanimationtransitionnone,//Normal status
Uiviewanimationtransitionflipfromleft,//flip from left to right
Uiviewanimationtransitionflipfromright,//flip from right to left
Uiviewanimationtransitioncurlup,//PAGE UP
Uiviewanimationtransitioncurldown,//Page Down
} uiviewanimationtransition;
typedef enum {
Uiviewanimationcurveeaseinout,
Uiviewanimationcurveeasein,
Uiviewanimationcurveeaseout,
Uiviewanimationcurvelinear
} Uiviewanimationcurve;
[UIView beginanimations:@ "ddd" context:nil]; Set animation
View.frame = CGRectMake (200, 200, 100, 100);
[UIView commitanimations]; Submit Animation
When the view moves from the original frame to the new frame, it slowly fades instead of just finishing the middle and adding it to the middle of the paragraph.
The following can also be added to [UIView beginanimations:@ "ddd" context:nil]; [UIView commitanimations];
View.transform = Cgaffinetransformmaketranslation (10, 10);//Set offset only once with respect to the original
View.transform = Cgaffinetransformtranslate (View.transform, 10, 10); Set offset offset multiple times
Self.view.transform = Cgaffinetransformmakerotation (M_PI);//Set rotation can only be rotated once
Self.view.transform = Cgaffinetransformrotate (Self.view.transform, M_PI); Rotate multiple times
Self.view.transform = Cgaffinetransformmakescale (1.1, 1.1); Setting the size can only change once the value is several times the original
Self.view.transform = Cgaffinetransformscale (Self.view.transform, 1.1, 1.1);//Change multiple times
Self.view.transform = cgaffinetransformidentity;//back to the original to execute once
Self.view.transform = Cgaffinetransforminvert (self.view.transform);//get opposite look size direction position executed multiple times


Here I implemented a custom animation method, easy to use, only need to call to achieve a good function.



Implementation of the method

-(void) Uiviewanimation: (uiview*) View frame: (CGRect) frame type: (int) type alpha: (float) Alpha Duration: (float) Duration

{

The corresponding parameter is implemented in the method, the call only need to input the parameters required in the method can be very good call this method, and realize the desired function!

[UIView Beginanimations:nil Context:nil];

[UIView setanimationduration:duration];

[UIView Setanimationcurve:type];

[UIView setanimationdelegate:self];

View.alpha=alpha;

View.frame=frame;

[UIView commitanimations];

}

Calling methods

[Self Uiviewanimation:downview frame:cgrectmake (0, height, +,] type:uiviewanimationcurveeaseout alpha:1 duration : 0.3];



Block mode



[UIView Animatewithduration:3 animations:^ (void) {
This is the equivalent of between Begin and Commint.
}completion:^ (BOOL finished) {
This is equivalent to the way the animation executes after execution, and you can continue nesting the block
}];
Advanced Block Animation (Next) inline

-(void) changeuiview{
[UIView animatewithduration:2 delay:0 options:uiviewanimationoptioncurveeaseout animations:^ (void) {
Moveview.alpha = 0.0;
}completion:^ (BOOL finished) {
[UIView animatewithduration:1 delay:1.0 Options:uiviewanimationoptionautoreverse | Uiviewanimationoptionrepeat animations:^ (void) {[UIView setanimationrepeatcount:2.5];
Moveview.alpha = 1.0;
}completion:^ (BOOL finished) {
}];
}];
}


Two:. Caanimation

Need to add libraries, and include header files

How many sub-categories are caanimation

Cabasicanimation

Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "opacity"];
@ "" In a variety of strings, you can find relevant information, be sure to fill in the right, the animation will do opacity set transparency bounds.size set size
[Animation Setfromvalue:[nsnumber numberwithfloat:1.0]; Set transparency from several beginnings
[Animation Settovalue:[nsnumber numberwithfloat:0.3]];//set Transparency to a few ends
[Animation setduration:0.1]; Set animation time
[Animation setrepeatcount:100000];//Set Repetition time
[Animation Setrepeatduration:4]; Will limit the number of repetitions
[Animation setautoreverses:no];//set whether from 1.0 to 0.3 again from 0.3 to 1.0 for one time if set to NO then 1.0 to 0.3 for one time
[Animation Setremovedoncompletion:yes]; Move out animation when finished the default is also
[View.layer addanimation:animation forkey:@ "abc"];//performing animations


Cakeyframeanimation

Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position"];//set view from initial position through a series of points
Nsarray *postionaraay = [Nsarray arraywithobjects:[nsvalue valuewithcgpoint:cgpointmake (+)], [NSValue Valuewithcgpoint:cgpointmake (],[nsvalue)
Valuewithcgpoint:cgpointmake (],[nsvalue valuewithcgpoint:cgpointmake)],[nsvalue valueWithCGPoint: Cgpointmake (0, +)],nil];//set point
Nsarray *times = [Nsarray arraywithobjects:[nsnumber numberwithfloat:0.3],[nsnumber NumberWithFloat:0.5],[NSNumber Numberwithfloat:0.6],[nsnumber Numberwithfloat:0.1],[nsnumber
Numberwithfloat:1.0], nil]; Set the time for the move process
[Animation Setkeytimes:times];
[Animation Setvalues:postionaraay];
[Animation Setduration:5]; Set animation time
[Bigimage.layer addanimation:animation forkey:@ "DD"]; Performing animations


Catransition

Catransition *animation = [catransition animation];
Animation.duration = 0.5f;
Animation.timingfunction = Uiviewanimationcurveeaseinout;
Animation.fillmode = Kcafillmodeforwards;
/*
Kcatransitionfade;
Kcatransitionmovein;
Kcatransitionpush;
Kcatransitionreveal;
*/
/*
Kcatransitionfromright;
Kcatransitionfromleft;
Kcatransitionfromtop;
Kcatransitionfrombottom;
*/
Animation.type = Kcatransitionpush;
Animation.subtype = Kcatransitionfrombottom;
[View.layer addanimation:animation forkey:animation];
Type can also be used directly with the string
/*
Cube
Suckeffect Roll Away
Oglflip Flip
Rippleeffect Water Waves
Pagecurl page
Pageuncurl
Camerairishollowopen
Camerairishollowclose
*/


Three: Nstimer

This is a timer to manipulate the animation method, he can combine the above method to achieve the diversification of animation!

-(void) OnTimer {
Imageview.center = Cgpointmake (imageview.center.x + delta.x,
Imageview.center.y + delta.y);
if (imageview.center.x > Self.view.bounds.size.width-ballradius | |
Imageview.center.x < Ballradius)
delta.x =-delta.x;
if (Imageview.center.y > Self.view.bounds.size.height-ballradius | |
Imageview.center.y < Ballradius)
DELTA.Y =-DELTA.Y;
}
-(void) Viewdidload {
Ballradius = IMAGEVIEW.BOUNDS.SIZE.WIDTH/2;
[Slider Setshowvalue:yes];
Delta = cgpointmake (12.0,4.0);
Timer = [Nstimer scheduledTimerWithTimeInterval:slider.value
Target:self
Selector: @selector (OnTimer)
Userinfo:nil
Repeats:yes];
[Super Viewdidload];
}
-(Ibaction) slidermoved: (ID) Sender {
[Timer invalidate];
Timer = [Nstimer scheduledTimerWithTimeInterval:slider.value
Target:self
Selector: @selector (OnTimer)
Userinfo:nil
Repeats:yes];
//
Timer = [Nstimer scheduledtimerwithtimeinterval:?
Invocation:?
Repeats:yes];
}

iOS development--Several methods of animation programming

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.