iOS animation effects and implementations

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/totogo2010/article/details/8501812

Animation effects provide a smooth user experience for State or page transitions, and in iOS we don't need to write our own animation code, and Core animation provides a rich API to achieve the animation you need.

Uikit only use UIView to show animations, animations support UIView These property changes below:

    • Frame
    • Bounds
    • Center
    • Transform
    • Alpha
    • BackgroundColor
    • Contentstretch
1. commitanimations mode using UIView animation [CPP]View PlainCopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];
  5. [Button settitle:@"Change" forstate:uicontrolstatenormal];
  6. Button.frame = CGRectMake (10, 10, 60, 40);
  7. [Button addtarget:self action: @selector (Changeuiview) forcontrolevents:uicontroleventtouchupinside];
  8. [Self.view Addsubview:button];
  9. }
  10. -(void) changeuiview{
  11. [UIView beginanimations:@"animation" context:nil];
  12. [UIView setanimationduration:1.0f];
  13. [UIView Setanimationcurve:uiviewanimationcurveeaseinout];
  14. [UIView setanimationtransition:uiviewanimationtransitionflipfromright ForView:self.view Cache:yes];
  15. [UIView commitanimations];
  16. }

Here are the effects of the click Change (two kinds): There are four types of animation constants [CPP]View PlainCopy
    1. Uiviewanimationtransitionnone,
    2. Uiviewanimationtransitionflipfromleft,
    3. Uiviewanimationtransitionflipfromright,
    4. Uiviewanimationtransitioncurlup,
    5. Uiviewanimationtransitioncurldown,

1.2 Swapping 2 view positions in the View controller

[self. View exchangesubviewatindex:1 withsubviewatindex:0];

Add two view first, one redview and one Yellowview

[CPP]View PlainCopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. UIView *redview = [[UIView alloc] initwithframe:[[uiscreen mainscreen] bounds]];
  5. Redview.backgroundcolor = [Uicolor Redcolor];
  6. [Self.view Addsubview:redview];
  7. UIView *yellowview = [[UIView alloc] initwithframe:[[uiscreen mainscreen] bounds]];
  8. Yellowview.backgroundcolor = [Uicolor Yellowcolor];
  9. [Self.view Addsubview:yellowview];
  10. UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];
  11. [Button settitle:@"Change" forstate:uicontrolstatenormal];
  12. Button.frame = CGRectMake (10, 10, 300, 40);
  13. [Button addtarget:self action: @selector (Changeuiview) forcontrolevents:uicontroleventtouchupinside];
  14. [Self.view Addsubview:button];
  15. UIButton *button1 = [UIButton buttonwithtype:uibuttontyperoundedrect];
  16. [Button1 settitle:@"Change 1" forstate:uicontrolstatenormal];
  17. Button1.frame = CGRectMake (10, 60, 300, 40);
  18. [Button1 addtarget:self Action: @selector (ChangeUIView1) forcontrolevents:uicontroleventtouchupinside];
  19. [Self.view Addsubview:button1];
  20. }

[CPP]View PlainCopy
    1. -  (void) changeuiview1{  
    2.     [uiview beginanimations:@
    3.     [uiview setanimationduration : 1.0f];  
    4.     [uiview setanimationcurve: uiviewanimationcurveeaseinout];  
    5.     [uiview  setanimationtransition:uiviewanimationtransitioncurldown forview:self.view cache:yes];  
    6.     //   swap 2 view positions in the View controller   
    7.     [self.view exchangesubviewatindex:1 withsubviewatindex:0];   
    8.     [uiview commitanimations];  
    9. }   

This looks like two pages.1.3, [UIView setanimationdidstopselector:@selector(animationfinish:)];

Before commitanimations the message, you can set the callback after the animation is complete, by:

[UIView setanimationdidstopselector:@selector(animationfinish:)];

2. Use: Catransition [CPP]View PlainCopy
    1. -(void) changeuiview2{
    2. Catransition *transition = [catransition animation];
    3. Transition.duration = 2.0f;
    4. Transition.type = Kcatransitionpush;
    5. Transition.subtype = Kcatransitionfromtop;
    6. [Self.view exchangesubviewatindex:1 withsubviewatindex:0];
    7. [Self.view.layer addanimation:transition forkey:@"animation"];
    8. }
The type of transition.type can have

Fade, push, uncover, cover

NSString * Const Kcatransitionfade;

NSString * Const Kcatransitionmovein;

NSString * Const Kcatransitionpush;

NSString * Const kcatransitionreveal;

These four species, Transition.subtype also have four kinds of

NSString * Const Kcatransitionfromright;

NSString * Const Kcatransitionfromleft;

NSString * Const Kcatransitionfromtop;

NSString * Const Kcatransitionfrombottom;


2.2 Types of animations of the private type:

Cube, absorb, flip, ripple, page turn, reverse flip, lens on, lens off

[CPP]View PlainCopy
    1. animation.type = @
    2. animation.type = @ "Suckeffect";    
    3. animation.type = @
    4. Animation.type = @ "Rippleeffect";    
    5. "Pagecurl";    
    6. Animation.type  = @ "Pageuncurl"   
    7. animation.type =  @ "camerairishollowopen ";   
    8. animation.type =  @ "camerairishollowclose ";   
Is the effect of the first cube cube: The Startprogress endprogress property of the 2.3 catransition property is of type float.
You can control the process of animation, which lets the animation rest on an animation point, with a value between 0.0 and 1.0. Endprogress is greater than or equal to startprogress. For example, the top of the cube to go, you can set endprogress= 0.5, so that the animation stays in the rotation of the general position. The above private animation effect, in the actual application to be cautious to use.  Because these animations may be rejected by the App Store when it is reviewed. 3, UIView + (void) Animatewithduration: (nstimeinterval) duration animations: (void (^) (void)) Animations completion: ( void (^) (BOOL finished)) Completion method. This method is only supported after the iOS4.0. The UIView method is simpler and easier to use than 1 miles. Add Moveview in Didview. [CPP]View PlainCopy
    1. Moveview = [[UIView alloc] Initwithframe:cgrectmake (10, 180, 200, 40)];
    2. Moveview.backgroundcolor = [Uicolor blackcolor];
    3. [Self.view Addsubview:moveview];

[CPP]View PlainCopy
  1. -(void) changeuiview3{
  2. [UIView animatewithduration:3 animations:^ (void) {
  3. Moveview.frame = CGRectMake (10, 270, 200, 40);
  4. }completion:^ (BOOL finished) {
  5. UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (20, 20, 40, 40)];
  6. Label.backgroundcolor = [Uicolor blackcolor];
  7. [Self.view Addsubview:label];
  8. }];
  9. }
Then move the animation with UIView Animatewithduration and add a label after the animation is finished.

3.2, animatewithduration of the use of nesting [CPP]View PlainCopy
  1. -(void) changeuiview3{
  2. [UIView Animatewithduration:2
  3. delay:0
  4. Options:uiviewanimationoptioncurveeaseout animations:^ (void) {
  5. Moveview.alpha = 0.0;
  6. }completion:^ (BOOL finished) {
  7. [UIView Animatewithduration:1
  8. delay:1.0
  9. Options:uiviewanimationoptionautoreverse | Uiviewanimationoptionrepeat
  10. animations:^ (void) {
  11. [UIView setanimationrepeatcount:2.5];
  12. Moveview.alpha = 1.0;
  13. }completion:^ (BOOL finished) {
  14. }];
  15. }];
  16. }
The effect of this nesting is to first turn the view into transparent, from transparent to opaque, repeat 2.5 times transparent to opaque effect. The code for the example in this article: Animatedemo

Jongfangzhi (http://blog.csdn.net/totogo2010)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

iOS animation effects and implementations

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.