Several methods of animation programming for iOS development
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 animationGeneral 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;//Return to the original appearance 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 from 1.0 to 0.3 again from 0.3 to 1.0 for once 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"];// Sets the view from the initial position through a series of points
- Nsarray *postionaraay = [Nsarray arraywithobjects:[nsvalue valuewithcgpoint: Cgpointmake (100, 20)], [Nsvalue Valuewithcgpoint:cgpointmake (max.)],[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:p ostionaraay];
- [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];
- }
Several methods of animation programming for iOS development