Sin descripción

FKCollectAnimationTool.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // FKCollectAnimationTool.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/9/10.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCollectAnimationTool.h"
  9. static NSString *COLLECT_ANIMATION_KEY = @"COLLECT_ANIMATION_KEY";
  10. @implementation FKCollectAnimationTool
  11. - (void)showAnimationWithCollect:(BOOL)collect inView:(UIView *)view{
  12. self.collectSelected = collect;
  13. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  14. if (collect) {
  15. UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"heart"]];
  16. [window.layer addSublayer:imageView.layer];
  17. imageView.layer.position = window.layer.position;
  18. imageView.layer.name = ANIMATION_LAYER_NAME;
  19. CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  20. CATransform3D transform1 = CATransform3DIdentity;
  21. CATransform3D transform2 = CATransform3DMakeScale(1.3f, 1.3f, 1.0f);
  22. CATransform3D transform3 = CATransform3DIdentity;
  23. anim.values = @[[NSValue valueWithCATransform3D:transform1], [NSValue valueWithCATransform3D:transform2], [NSValue valueWithCATransform3D:transform3]];
  24. anim.removedOnCompletion = NO;
  25. anim.fillMode = kCAFillModeForwards;
  26. CAKeyframeAnimation *alphaAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
  27. alphaAnim.values = @[@0.8, @1.0, @0];
  28. alphaAnim.removedOnCompletion = NO;
  29. alphaAnim.fillMode = kCAFillModeForwards;
  30. CAAnimationGroup *group = [CAAnimationGroup animation];
  31. group.animations = @[anim, alphaAnim];
  32. group.duration = 0.7f;
  33. group.removedOnCompletion = NO;
  34. group.fillMode = kCAFillModeForwards;
  35. group.delegate = self;
  36. [imageView.layer addAnimation:group forKey:COLLECT_ANIMATION_KEY];
  37. }else{
  38. UIImageView *leftImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftHeart"]];
  39. UIImageView *rightImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rightHeart"]];
  40. [window.layer addSublayer:leftImgView.layer];
  41. [window.layer addSublayer:rightImgView.layer];
  42. leftImgView.layer.name = ANIMATION_LAYER_NAME;
  43. rightImgView.layer.name = ANIMATION_LAYER_NAME;
  44. CGFloat leftH = leftImgView.intrinsicContentSize.height;
  45. CGFloat rightH = rightImgView.intrinsicContentSize.height;
  46. leftImgView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);
  47. rightImgView.layer.anchorPoint = CGPointMake(0, 1.0f);
  48. leftImgView.layer.position = CGPointMake(window.layer.position.x, window.layer.position.y + leftH / 2);
  49. rightImgView.layer.position = CGPointMake(window.layer.position.x, window.layer.position.y + rightH / 2);
  50. CABasicAnimation *leftRotaAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
  51. leftRotaAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  52. leftRotaAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(- M_PI_4, 0, 0, 1.0)];
  53. leftRotaAnim.removedOnCompletion = NO;
  54. leftRotaAnim.fillMode = kCAFillModeForwards;
  55. CABasicAnimation *leftAlphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
  56. leftAlphaAnim.fromValue = @1.0;
  57. leftAlphaAnim.toValue = @0;
  58. leftAlphaAnim.removedOnCompletion = NO;
  59. leftAlphaAnim.fillMode = kCAFillModeForwards;
  60. CAAnimationGroup *leftGroup = [CAAnimationGroup animation];
  61. leftGroup.animations = @[leftRotaAnim, leftAlphaAnim];
  62. leftGroup.duration = 0.5f;
  63. leftGroup.removedOnCompletion = NO;
  64. leftGroup.fillMode = kCAFillModeForwards;
  65. leftGroup.delegate = self;
  66. CABasicAnimation *rightRotaAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
  67. rightRotaAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  68. rightRotaAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1.0)];
  69. rightRotaAnim.removedOnCompletion = NO;
  70. rightRotaAnim.fillMode = kCAFillModeForwards;
  71. CABasicAnimation *rightAlphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
  72. rightAlphaAnim.fromValue = @1.0;
  73. rightAlphaAnim.toValue = @0;
  74. rightAlphaAnim.removedOnCompletion = NO;
  75. rightAlphaAnim.fillMode = kCAFillModeForwards;
  76. CAAnimationGroup *rightGroup = [CAAnimationGroup animation];
  77. rightGroup.animations = @[rightRotaAnim, rightAlphaAnim];
  78. rightGroup.duration = 0.5f;
  79. rightGroup.removedOnCompletion = NO;
  80. rightGroup.fillMode = kCAFillModeForwards;
  81. rightGroup.delegate = self;
  82. [leftImgView.layer addAnimation:leftGroup forKey:COLLECT_ANIMATION_KEY];
  83. [rightImgView.layer addAnimation:rightGroup forKey:COLLECT_ANIMATION_KEY];
  84. }
  85. }
  86. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  87. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  88. CALayer *targetLayer = nil;
  89. for (CALayer *layer in window.layer.sublayers) {
  90. if ([layer animationForKey:COLLECT_ANIMATION_KEY] == anim){
  91. targetLayer = layer;
  92. break;
  93. }
  94. }
  95. if (targetLayer) [targetLayer removeFromSuperlayer];
  96. BOOL finished = YES;
  97. for (CALayer *layer in window.layer.sublayers) {
  98. if ([layer.name isEqualToString:ANIMATION_LAYER_NAME]) {
  99. finished = NO;
  100. break;
  101. }
  102. }
  103. // window 上没有动画了,执行delegate
  104. if (finished) {
  105. if ([self.delegate respondsToSelector:@selector(allCollectAnimationFinish:)]){
  106. [self.delegate allCollectAnimationFinish:self];
  107. }
  108. }
  109. }
  110. @end