RocketViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // RocketViewController.m
  3. // animatedemo
  4. //
  5. // Created by 7yword on 16/7/13.
  6. // Copyright © 2016年 7yworld. All rights reserved.
  7. //
  8. #import "RocketViewController.h"
  9. @interface RocketViewController (){
  10. NSTimer * timer;
  11. int cc;
  12. }
  13. @property (weak, nonatomic) IBOutlet UIView *rocketContrainerView;
  14. @property (weak, nonatomic) IBOutlet UIImageView *rocketImgView;
  15. @property (weak, nonatomic) IBOutlet UIImageView *effect;
  16. @property (weak, nonatomic) IBOutlet UILabel *count;
  17. @end
  18. @implementation RocketViewController
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = kClearColor;
  23. _senderNameLabel.textColor = kTextColorSenderName;
  24. _senderNameLabel.text = _senderNameStr;
  25. _rocketContrainerView.hidden = YES;
  26. _count.textColor = [UIColor colorWithRed:238 / 255.0f green:201 / 255.0f blue:0 alpha:1.0];
  27. cc = 3;
  28. timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(count_1:) userInfo:nil repeats:YES];
  29. }
  30. - (void)count_1:(id)sender
  31. {
  32. if(cc > 0)
  33. {
  34. _count.text = [NSString stringWithFormat:@"%i",cc];
  35. CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  36. scaleAnimation.fromValue = [NSNumber numberWithFloat:3.0f];
  37. scaleAnimation.toValue = [NSNumber numberWithFloat:0.3f];
  38. scaleAnimation.duration = 1.0f;
  39. scaleAnimation.removedOnCompletion = YES;
  40. scaleAnimation.repeatCount = 1;
  41. scaleAnimation.delegate = self;
  42. [scaleAnimation setValue:@"scaleAnimation" forKey:@"scaleAnimation"];
  43. [_count.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
  44. cc--;
  45. }
  46. else
  47. {
  48. [timer invalidate];
  49. timer = nil;
  50. _rocketContrainerView.hidden = NO;
  51. [self do_move:_rocketContrainerView];
  52. }
  53. }
  54. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  55. {
  56. __weak typeof(self) weakSelf = self;
  57. if ([[anim valueForKey:@"moveAnimation"] isEqualToString:@"moveAnimation"])
  58. {
  59. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  60. [weakSelf do_effect:_effect];
  61. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  62. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width / 2.0f, -50)];
  63. moveAnimation.duration = 3.0f;
  64. moveAnimation.removedOnCompletion = NO;
  65. moveAnimation.fillMode = kCAFillModeForwards;
  66. moveAnimation.repeatCount = 1;
  67. moveAnimation.delegate = self;
  68. [moveAnimation setValue:@"moveAnimation1" forKey:@"moveAnimation1"];
  69. [weakSelf.rocketContrainerView.layer addAnimation:moveAnimation forKey:@"moveAnimation1"];
  70. });
  71. }
  72. else if([[anim valueForKey:@"scaleAnimation"] isEqualToString:@"scaleAnimation"] && cc == 0)
  73. {
  74. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  75. weakSelf.count.hidden = YES;
  76. });
  77. }
  78. else if ([[anim valueForKey:@"moveAnimation1"] isEqualToString:@"moveAnimation1"])
  79. {
  80. if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(rocketAnimationFinished)])
  81. {
  82. [weakSelf.delegate rocketAnimationFinished];
  83. [self.view removeFromSuperview];
  84. }
  85. }
  86. }
  87. #pragma mark - 平移动画
  88. - (void)do_move:(UIView *)view
  89. {
  90. CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  91. moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([[UIScreen mainScreen] bounds].size.width / 2.0f, [[UIScreen mainScreen] bounds].size.height / 2.0f)];
  92. moveAnimation.duration = 3.0f;
  93. moveAnimation.removedOnCompletion = NO;
  94. moveAnimation.repeatCount = 1;
  95. moveAnimation.delegate = self;
  96. moveAnimation.fillMode = kCAFillModeForwards;
  97. [moveAnimation setValue:@"moveAnimation" forKey:@"moveAnimation"];
  98. [view.layer addAnimation:moveAnimation forKey:@"moveAnimation"];
  99. _rocketImgView.animationImages = @[[UIImage imageNamed:@"fw_gift_rocket3"],[UIImage imageNamed:@"fw_gift_rocket2"]];
  100. _rocketImgView.animationDuration = 0.8f;
  101. _rocketImgView.animationRepeatCount = INT_MAX;
  102. [_rocketImgView startAnimating];
  103. }
  104. - (void)do_effect:(UIImageView *)view
  105. {
  106. CABasicAnimation * alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  107. alphaAnimation.toValue = [NSNumber numberWithFloat:1.0f];
  108. alphaAnimation.duration = 1.0f;
  109. alphaAnimation.removedOnCompletion = NO;
  110. alphaAnimation.repeatCount = 1;
  111. alphaAnimation.autoreverses = YES;
  112. alphaAnimation.fillMode = kCAFillModeForwards;
  113. [alphaAnimation setValue:@"alphaAnimation" forKey:@"alphaAnimation"];
  114. [view.layer addAnimation:alphaAnimation forKey:@"alphaAnimation"];
  115. /*
  116. CABasicAnimation * alphaAnimation1 = [CABasicAnimation animationWithKeyPath:@"scale"];
  117. alphaAnimation1.toValue = [NSNumber numberWithFloat:2.0f];
  118. // alphaAnimation1.duration = 3.0f;
  119. alphaAnimation1.removedOnCompletion = NO;
  120. alphaAnimation1.repeatCount = 1;
  121. alphaAnimation1.autoreverses = YES;
  122. alphaAnimation1.fillMode = kCAFillModeForwards;
  123. [alphaAnimation1 setValue:@"alphaAnimation1" forKey:@"alphaAnimation1"];
  124. // CABasicAnimation * alphaAnimation2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
  125. // alphaAnimation2.toValue = [NSNumber numberWithFloat:1.0f];
  126. // alphaAnimation2.duration = 3.0f;
  127. // alphaAnimation2.removedOnCompletion = NO;
  128. // alphaAnimation2.repeatCount = 1;
  129. // alphaAnimation2.autoreverses = YES;
  130. // alphaAnimation2.fillMode = kCAFillModeForwards;
  131. // [alphaAnimation2 setValue:@"alphaAnimation2" forKey:@"alphaAnimation2"];
  132. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  133. animationGroup.duration = 3;
  134. animationGroup.autoreverses = YES;
  135. animationGroup.repeatCount = 1;
  136. animationGroup.animations =[NSArray arrayWithObjects:alphaAnimation, alphaAnimation1, nil];
  137. [view.layer addAnimation:animationGroup forKey:@"animationGroup"];
  138. // [view.layer addAnimation:alphaAnimation forKey:@"alphaAnimation"];
  139. */
  140. }
  141. - (void) shakeToShow:(UIView*)aView
  142. {
  143. CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  144. animation.duration = 0.5;
  145. NSMutableArray *values = [NSMutableArray array];
  146. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
  147. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
  148. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
  149. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
  150. animation.values = values;
  151. [aView.layer addAnimation:animation forKey:nil];
  152. }
  153. @end