iOS simple animation effects: flashing, moving, rotating, path, combo

Source: Internet
Author: User

  1. #define Kdegreestoradian (x) (M_PI * (x)/180.0)
  2. #define Kradiantodegrees (Radian) (radian*180.0)/(M_PI)
  3. -(void) viewdidload
  4. {
  5. [Superviewdidload];
  6. Self.title = @ "Test animation";
  7. Self.view.backgroundColor = [Uicolorlightgraycolor];
  8. MyTest1 = [[Uilabelalloc]initwithframe:cgrectmake (10, 100, 60, 40)];
  9. Mytest1.backgroundcolor = [Uicolorbluecolor];
  10. Mytest1.textalignment = Nstextalignmentcenter;
  11. Mytest1.text = @ "Zhang Mingwei";
  12. Mytest1.textcolor = [Uicolorwhitecolor];
  13. [Self.viewaddSubview:myTest1];
  14. Flashing effect.
  15. [Mytest1.layer addanimation:[self opacityforever_animation:0.5] forkey:nil];
  16. Move the animation.
  17. [Mytest1.layer addanimation:[self movex:1.0f x:[nsnumber numberwithfloat:200.0f]] forkey:nil];
  18. The zoom effect.
  19. [Mytest1.layer addanimation:[self scale:[nsnumber numberwithfloat:1.0f] Orgin:[nsnumber numberWithFloat:3.0f] durtimes:2.0f Rep:maxfloat] Forkey:nil];
  20. Combining animations.
  21. Nsarray *myarray = [Nsarray arraywithobjects:[self opacityforever_animation:0.5],[self moveX:1.0f X:[NSNumber number Withfloat:200.0f]],[self scale:[nsnumber numberwithfloat:1.0f] Orgin:[nsnumber numberwithfloat:3.0f] durTimes:2.0f Rep:maxfloat], nil];
  22. [Mytest1.layer addanimation:[self groupanimation:myarray durtimes:3.0f rep:maxfloat] forKey:nil];
  23. Path animation.
  24. Cgmutablepathref Mypah = cgpathcreatemutable ();
  25. Cgpathmovetopoint (Mypah, nil,30, 77);
  26. Cgpathaddcurvetopoint (Mypah, Nil, 50, 50, 60, 200, 200, 200);//Here is the control point.
  27. [Mytest1.layer addanimation:[self keyframeanimation:mypah durtimes:5 rep:maxfloat] forKey:nil];
  28. Rotate the animation.
  29. [Mytest1.layeraddanimation:[selfrotation:2degree:kradiantodegrees (Direction:1repeatcount:maxfloat] ForKey: NIL];
  30. }
  31. #pragma mark = = Perpetual flashing animated ======
  32. -(Cabasicanimation *) Opacityforever_animation: (float) time
  33. {
  34. Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "opacity"];//must write opacity.
  35. Animation.fromvalue = [NSNumber numberwithfloat:1.0f];
  36. Animation.tovalue = [NSNumber numberwithfloat:0.0f];//this is transparency.
  37. animation.autoreverses = YES;
  38. Animation.duration = time;
  39. Animation.repeatcount = maxfloat;
  40. Animation.removedoncompletion = NO;
  41. Animation.fillmode = Kcafillmodeforwards;
  42. Animation.timingfunction=[camediatimingfunction functionwithname:kcamediatimingfunctioneasein];///No words are evenly animated.
  43. return animation;
  44. }
  45. #pragma mark ===== Horizontal, vertical movement ===========
  46. -(Cabasicanimation *) MoveX: (float) time x: (NSNumber *) x
  47. {
  48. Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "transform.translation.x"];///.y then move down.
  49. Animation.tovalue = x;
  50. Animation.duration = time;
  51. Animation.removedoncompletion = No;//yes, and return to the original position.
  52. Animation.repeatcount = maxfloat;
  53. Animation.fillmode = Kcafillmodeforwards;
  54. return animation;
  55. }
  56. #pragma mark ===== Zoom-=============
  57. -(Cabasicanimation *) Scale: (NSNumber *) Multiple orgin: (NSNumber *) orginmultiple durtimes: (float) time Rep: (float) Reperttimes
  58. {
  59. Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "Transform.scale"];
  60. Animation.fromvalue = multiple;
  61. Animation.tovalue = Orginmultiple;
  62. animation.autoreverses = YES;
  63. Animation.repeatcount = Reperttimes;
  64. Animation.duration = time;//When not set, there is a default zoom time.
  65. Animation.removedoncompletion = NO;
  66. Animation.fillmode = Kcafillmodeforwards;
  67. return animation;
  68. }
  69. #pragma mark ===== Combo animation-=============
  70. -(Caanimationgroup *) Groupanimation: (Nsarray *) animationary durtimes: (float) time Rep: (float) repeattimes
  71. {
  72. Caanimationgroup *animation = [Caanimationgroupanimation];
  73. Animation.animations = animationary;
  74. Animation.duration = time;
  75. Animation.removedoncompletion = NO;
  76. Animation.repeatcount = RepeatTimes;
  77. Animation.fillmode = Kcafillmodeforwards;
  78. return animation;
  79. }
  80. #pragma mark ===== Path animation-=============
  81. -(Cakeyframeanimation *) Keyframeanimation: (cgmutablepathref) path durtimes: (float) time Rep: (float) repeattimes
  82. {
  83. Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position"];
  84. Animation.path = path;
  85. Animation.removedoncompletion = NO;
  86. Animation.fillmode = Kcafillmodeforwards;
  87. Animation.timingfunction = [Camediatimingfunction Functionwithname:kcamediatimingfunctioneasein];
  88. animation.autoreverses = NO;
  89. Animation.duration = time;
  90. Animation.repeatcount = RepeatTimes;
  91. return animation;
  92. }
  93. #pragma mark = = Rotation animation ======
  94. -(Cabasicanimation *) Rotation: (float) dur degree: (float) degree direction: (int) Direction repeatcount: (int) RepeatCount
  95. {
  96. Catransform3d rotationtransform = catransform3dmakerotation (degree, 0, 0, direction);
  97. Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "transform"];
  98. Animation.tovalue = [Nsvalue valuewithcatransform3d:rotationtransform];
  99. Animation.duration = dur;
  100. animation.autoreverses = NO;
  101. Animation.cumulative = NO;
  102. Animation.fillmode = Kcafillmodeforwards;
  103. Animation.repeatcount = RepeatCount;
  104. Animation.delegate = self;
  105. return animation;
  106. }

iOS simple animation effects: flashing, moving, rotating, path, combo

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.