FerrariController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // FerrariController.m
  3. // animatedemo
  4. //
  5. // Created by 7yword on 16/7/13.
  6. // Copyright © 2016年 7yworld. All rights reserved.
  7. //
  8. #import "FerrariController.h"
  9. @interface FerrariController ()
  10. @property (weak, nonatomic) IBOutlet UIView *car_body;
  11. @property (weak, nonatomic) IBOutlet UIImageView *front;
  12. @property (weak, nonatomic) IBOutlet UIImageView *back;
  13. @property (weak, nonatomic) IBOutlet UIView *back_car_view;
  14. @property (weak, nonatomic) IBOutlet UIImageView *back_back;
  15. @property (weak, nonatomic) IBOutlet UIImageView *back_front;
  16. @property (weak, nonatomic) IBOutlet UIImageView *back_body;
  17. @end
  18. @implementation FerrariController
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = kClearColor;
  23. _senderNameLabel1.textColor = kTextColorSenderName;
  24. _senderNameLabel2.textColor = kTextColorSenderName;
  25. _senderNameLabel1.text = _senderNameStr1;
  26. _senderNameLabel2.text = _senderNameStr2;
  27. [_back_car_view bringSubviewToFront:_back_body];
  28. [_back_car_view setHidden:YES];
  29. [self do_rotation:_front];
  30. [self do_rotation:_back];
  31. [self do_move:_car_body];
  32. }
  33. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  34. {
  35. NSString * value = [anim valueForKey:@"moveAnimation"];
  36. if ([value isEqualToString:@"moveAnimation"])
  37. {
  38. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  39. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  40. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0, [[UIScreen mainScreen] bounds].size.height + 50)];
  41. moveAnimation.duration = 3.0f;
  42. moveAnimation.removedOnCompletion = NO;
  43. moveAnimation.fillMode = kCAFillModeForwards;
  44. moveAnimation.repeatCount = 1;
  45. moveAnimation.delegate = self;
  46. [moveAnimation setValue:@"moveAnimation1" forKey:@"moveAnimation1"];
  47. [_car_body.layer addAnimation:moveAnimation forKey:@"moveAnimation1"];
  48. });
  49. }
  50. else if([[anim valueForKey:@"moveAnimation1"] isEqualToString:@"moveAnimation1"])
  51. {
  52. [_car_body setHidden:YES];
  53. [_back_car_view setHidden:NO];
  54. CABasicAnimation* rotationAnimation1;
  55. rotationAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  56. rotationAnimation1.toValue = [NSNumber numberWithFloat: M_PI * 1.0];
  57. rotationAnimation1.duration = 0.2f;
  58. rotationAnimation1.cumulative = YES;
  59. rotationAnimation1.repeatCount = INT_MAX;
  60. [_back_front.layer addAnimation:rotationAnimation1 forKey:@"rotationAnimation"];
  61. CABasicAnimation* rotationAnimation2;
  62. rotationAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  63. rotationAnimation2.toValue = [NSNumber numberWithFloat: M_PI * 1.0];
  64. rotationAnimation2.duration = 0.2f;
  65. rotationAnimation2.cumulative = YES;
  66. rotationAnimation2.repeatCount = INT_MAX;
  67. [_back_back.layer addAnimation:rotationAnimation2 forKey:@"rotationAnimation"];
  68. // _back_back.animationImages = @[[UIImage imageNamed:@"fw_gift_ferrari_tyre2"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_2"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_3"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_4"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_5"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_6"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_7"],[UIImage imageNamed:@"fw_gift_ferrari_tyre2_8"]];
  69. // _back_back.animationDuration = 0.5f;
  70. // _back_back.animationRepeatCount = INT_MAX;
  71. // [_back_back startAnimating];
  72. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  73. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width, -50)];
  74. moveAnimation.duration = 3.0f;
  75. moveAnimation.removedOnCompletion = NO;
  76. moveAnimation.repeatCount = 1;
  77. moveAnimation.fillMode = kCAFillModeForwards;
  78. moveAnimation.delegate = self;
  79. [moveAnimation setValue:@"moveAnimation2" forKey:@"moveAnimation2"];
  80. [_back_car_view.layer addAnimation:moveAnimation forKey:@"moveAnimation2"];
  81. }
  82. else if([[anim valueForKey:@"moveAnimation2"] isEqualToString:@"moveAnimation2"])
  83. {
  84. if (_delegate && [_delegate respondsToSelector:@selector(ferrariAnimationFinished)])
  85. {
  86. [_delegate ferrariAnimationFinished];
  87. [self.view removeFromSuperview];
  88. }
  89. }
  90. }
  91. #pragma mark - 平移动画
  92. - (void)do_move:(UIView *)view
  93. {
  94. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  95. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width / 2.0f, [[UIScreen mainScreen] bounds].size.height / 2.0f)];
  96. moveAnimation.duration = 3.0f;
  97. moveAnimation.removedOnCompletion = NO;
  98. moveAnimation.repeatCount = 1;
  99. moveAnimation.delegate = self;
  100. moveAnimation.fillMode = kCAFillModeForwards;
  101. [moveAnimation setValue:@"moveAnimation" forKey:@"moveAnimation"];
  102. [view.layer addAnimation:moveAnimation forKey:@"moveAnimation"];
  103. }
  104. #pragma mark - 旋转动画
  105. - (void)do_rotation:(UIView *)view
  106. {
  107. CABasicAnimation* rotationAnimation;
  108. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  109. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * -1.0];
  110. rotationAnimation.duration = 0.2f;
  111. rotationAnimation.cumulative = YES;
  112. rotationAnimation.repeatCount = INT_MAX;
  113. [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  114. }
  115. @end