Several methods of animation programming for iOS development

Source: Internet
Author: User

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
  1. [UIView beginanimations: @ "ddd" context:nil];//set animation
  2. [UIView commitanimations]; Submit Animation
  3. These two are necessary and then add the animated code in the middle of the two sentences
  4. [UIView beginanimations:@ "DDD" context:nil];//set animated DDD to animated name
  5. [UIView setanimationduration: 3];//defines the duration of the animation
  6. [UIView setanimationcurve: uiviewanimationcurveeaseinout]; Setanimationcurve to define animation acceleration or deceleration mode
  7. [UIView setanimationtransition: Uiviewanimationtransitioncurldown ForView:self.window Cache:yes];
  8. Sets the style of the animation Forview for which view implements this animation effect
  9. [UIView Setanimationdelay: 3]; Set how long animation delay is performed
  10. [UIView setanimationdelegate: self]; Animating the agent implementation before and after the animation method is set before Commitanimation
  11. [UIView setanimationdidstopselector: @selector (stop)];//sets the method to be executed after the animation finishes
  12. [UIView setanimationwillstartselector: @selector (Star)];//set the way the animation will start executing
  13. [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;
  1. [UIView beginanimations:@ "ddd" context:nil]; Set animation
  2. View.frame = CGRectMake (200, 200, 100, 100);
  3. [UIView commitanimations]; Submit Animation
  4. 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.
  5. The following can also be added to [UIView beginanimations:@ "ddd" context:nil]; [UIView commitanimations];
  6. View.transform = cgaffinetransformmaketranslation(10, 10);//Set offset only once with respect to the original
  7. View.transform = cgaffinetransformtranslate(View.transform, 10, 10); set offset offset multiple times
  8. Self.view.transform = cgaffinetransformmakerotation(M_PI);//Set rotation can only be rotated once
  9. Self.view.transform = cgaffinetransformrotate(Self.view.transform, M_PI); Rotate multiple times
  10. Self.view.transform = Cgaffinetransformmakescale(1.1, 1.1); Setting the size can only change once the value is several times the original
  11. Self.view.transform = Cgaffinetransformscale(Self.view.transform, 1.1, 1.1);//Change multiple times
  12. Self.view.transform = cgaffinetransformidentity;//Return to the original appearance to execute once
  13. 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

    1. [UIView animatewithduration:3 animations:^ (void) {
    2. This is the equivalent of between Begin and Commint.
    3. }completion:^ (BOOL finished) {
    4. This is equivalent to the way the animation executes after execution, and you can continue nesting the block
    5. }];

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
  1. Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "opacity"];
  2. @ "" 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
  3. [Animation setfromvalue: [NSNumber numberwithfloat:1.0]]; Set transparency from several beginnings
  4. [Animation settovalue: [nsnumber numberwithfloat:0.3]];//set transparency to a few ends
  5. [Animation setduration: 0.1]; Set animation time
  6. [Animation Setrepeatcount: 100000];//Set repetition time
  7. [Animation setrepeatduration: 4]; Will limit the number of repetitions
  8. [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
  9. [Animation setremovedoncompletion: YES]; Move out animation when finished the default is also
  10. [View.layer addanimation:animation forkey:@ "abc"];//performing animations

cakeyframeanimation
  1. Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position"];// Sets the view from the initial position through a series of points
  2. Nsarray *postionaraay = [Nsarray arraywithobjects:[nsvalue valuewithcgpoint: Cgpointmake (100, 20)], [Nsvalue Valuewithcgpoint:cgpointmake (max.)],[nsvalue
  3. Valuewithcgpoint:cgpointmake (],[nsvalue valuewithcgpoint:cgpointmake)],[nsvalue valueWithCGPoint: Cgpointmake (0, +)],nil];//set point
  4. Nsarray *times = [Nsarray arraywithobjects:[nsnumber numberwithfloat:0.3],[nsnumber Numberwithfloat:0.5],[nsnumber Numberwithfloat:0.6],[nsnumber Numberwithfloat:0.1],[nsnumber
  5. Numberwithfloat:1.0], nil]; Set the time for the move process
  6. [Animation Setkeytimes: times];
  7. [Animation setvalues:p ostionaraay];
  8. [Animation setduration: 5]; Set animation time
  9. [Bigimage.layer addanimation: Animation forkey:@ "DD"]; Performing animations

catransition
    1. Catransition *animation = [catransition animation];
    2. Animation. duration = 0.5f;
    3. Animation. timingfunction = uiviewanimationcurveeaseinout;
    4. Animation. Fillmode = kcafillmodeforwards;
    • /*
    • Kcatransitionfade;
    • Kcatransitionmovein;
    • Kcatransitionpush;
    • Kcatransitionreveal;
    • */
    • /*
    • Kcatransitionfromright;
    • Kcatransitionfromleft;
    • Kcatransitionfromtop;
    • Kcatransitionfrombottom;
    • */
    1. Animation. type = Kcatransitionpush;
    2. Animation. subtype = Kcatransitionfrombottom;
    3. [View.layer addanimation: Animation forkey:animation];
    4. 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!

  1. -(void) OnTimer {
  2. Imageview.center = Cgpointmake (imageview.center.x + delta.x,
  3. Imageview.center.y + delta.y);
  4. if (imageview.center.x > Self.view.bounds.size.width-ballradius | |
  5. Imageview.center.x < Ballradius)
  6. delta.x =-delta.x;
  7. if (Imageview.center.y > Self.view.bounds.size.height-ballradius | |
  8. Imageview.center.y < Ballradius)
  9. DELTA.Y =-DELTA.Y;
  10. }
  11. -(void) Viewdidload {
  12. Ballradius = IMAGEVIEW.BOUNDS.SIZE.WIDTH/2;
  13. [Slider Setshowvalue:yes];
  14. Delta = cgpointmake (12.0,4.0);
  15. Timer = [Nstimer scheduledTimerWithTimeInterval:slider.value
  16. Target:self
  17. Selector: @selector (OnTimer)
  18. Userinfo:nil
  19. Repeats:yes];
  20. [Super Viewdidload];
  21. }
  22. -(Ibaction) slidermoved: (ID) Sender {
  23. [Timer invalidate];
  24. Timer = [Nstimer scheduledTimerWithTimeInterval:slider.value
  25. Target:self
  26. Selector: @selector (OnTimer)
  27. Userinfo:nil
  28. Repeats:yes];
  29. //
    1. Timer = [Nstimer scheduledtimerwithtimeinterval:?
    2. Invocation:?
    3. Repeats:yes];
  30. }

Several methods of animation programming for iOS development

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.