iOS简单动画效果:闪烁、移动、旋转、路径、组合

时间:2023-03-09 07:19:20
iOS简单动画效果:闪烁、移动、旋转、路径、组合
  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 = @"测试动画";
  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 = @"张明炜";
  12. myTest1.textColor = [UIColorwhiteColor];
  13. [self.viewaddSubview:myTest1];
  14. //闪烁效果。
  15. //    [myTest1.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];
  16. ///移动的动画。
  17. //    [myTest1.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]] forKey:nil];
  18. //缩放效果。
  19. //    [myTest1.layer addAnimation:[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT] forKey:nil];
  20. //组合动画。
  21. //    NSArray *myArray = [NSArray arrayWithObjects:[self opacityForever_Animation:0.5],[self moveX:1.0f X:[NSNumber numberWithFloat: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. //路径动画。
  24. //    CGMutablePathRef myPah = CGPathCreateMutable();
  25. //    CGPathMoveToPoint(myPah, nil,30, 77);
  26. //    CGPathAddCurveToPoint(myPah, nil, 50, 50, 60, 200, 200, 200);//这里的是控制点。
  27. //    [myTest1.layer addAnimation:[self keyframeAnimation:myPah durTimes:5 Rep:MAXFLOAT] forKey:nil];
  28. //旋转动画。
  29. [myTest1.layeraddAnimation:[selfrotation:2degree:kRadianToDegrees(90) direction:1repeatCount:MAXFLOAT] forKey:nil];
  30. }
  31. #pragma mark === 永久闪烁的动画 ======
  32. -(CABasicAnimation *)opacityForever_Animation:(float)time
  33. {
  34. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
  35. animation.fromValue = [NSNumber numberWithFloat:1.0f];
  36. animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
  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];///没有的话是均匀的动画。
  43. return animation;
  44. }
  45. #pragma mark =====横向、纵向移动===========
  46. -(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x
  47. {
  48. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];///.y的话就向下移动。
  49. animation.toValue = x;
  50. animation.duration = time;
  51. animation.removedOnCompletion = NO;//yes的话,又返回原位置了。
  52. animation.repeatCount = MAXFLOAT;
  53. animation.fillMode = kCAFillModeForwards;
  54. return animation;
  55. }
  56. #pragma mark =====缩放-=============
  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;//不设置时候的话,有一个默认的缩放时间.
  65. animation.removedOnCompletion = NO;
  66. animation.fillMode = kCAFillModeForwards;
  67. return  animation;
  68. }
  69. #pragma mark =====组合动画-=============
  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 =====路径动画-=============
  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 ====旋转动画======
  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. }