Нет описания

FKAddBasketAnimTool.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // FKAddBasketAnimation.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/9.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKAddBasketAnimTool.h"
  9. static NSString *SCALE_ANIM_IDENTIFY = @"SCALE_ANIM_IDENTIFY";
  10. static NSString *GROUP_ANIM_IDENTIFY = @"GROUP_ANIM_IDENTIFY";
  11. static NSString *MOVE_POINT_KEY = @"MOVE_POINT_KEY";
  12. @interface FKAddBasketAnimTool ()
  13. @property (nonatomic, strong) UIImageView *imageView;
  14. @property (nonatomic, strong) CABasicAnimation *scaleAnim;
  15. @property (nonatomic, strong) CAAnimationGroup *animGroup;
  16. @property (nonatomic, assign) BOOL removedOnComplition;
  17. @end
  18. @implementation FKAddBasketAnimTool
  19. - (instancetype)init{
  20. if (self = [super init]) {
  21. self.scale = 4.0f;
  22. self.scaleDuration = 0.4f;
  23. self.moveDuration = 0.4f;
  24. self.removedOnComplition = YES;
  25. self.margin = 30.0f;
  26. }
  27. return self;
  28. }
  29. - (void)showInView:(UIView *)inView targetView:(UIView *)targetView{
  30. if (!inView || !targetView) return;
  31. CGPoint centerPoint = inView.center;
  32. CGRect targetFrame = [targetView convertRect:targetView.bounds toView:inView];
  33. CGPoint targetPoint = CGPointMake(CGRectGetMidX(targetFrame), CGRectGetMidY(targetFrame));
  34. CGPoint moveTarget = CGPointMake(targetPoint.x - centerPoint.x, targetPoint.y - centerPoint.y);
  35. [self.scaleAnim setValue:[NSValue valueWithCGPoint:moveTarget] forKey:MOVE_POINT_KEY];
  36. [inView addSubview:self.imageView];
  37. self.imageView.center = inView.center;
  38. if (self.image){
  39. self.imageView.image = self.image;
  40. }else if (self.picUrl.length){
  41. [self.imageView setImageWithURL:self.picUrl cdnWidth:80];
  42. }
  43. [self.imageView.layer addAnimation:self.scaleAnim forKey:SCALE_ANIM_IDENTIFY];
  44. }
  45. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  46. // NSString *identify = [[anim valueForKey:ANIMTIOAN_IDENTIFY_KEY] stringValue];
  47. CAAnimation *scaleAnim = [self.imageView.layer animationForKey:SCALE_ANIM_IDENTIFY];
  48. CAAnimation *groupAnim = [self.imageView.layer animationForKey:GROUP_ANIM_IDENTIFY];
  49. if (anim == scaleAnim && flag){
  50. CGPoint movePoint = [[self.scaleAnim valueForKey:MOVE_POINT_KEY] CGPointValue];
  51. CATransform3D trans2 = CATransform3DMakeTranslation(movePoint.x, movePoint.y, 0);
  52. //同一keypath只能保留一个animation状态
  53. self.imageView.layer.transform = CATransform3DMakeScale(self.scale, self.scale, 1.0f);
  54. CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform"];
  55. anim1.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
  56. anim1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)];
  57. anim1.duration = self.moveDuration;
  58. anim1.removedOnCompletion = NO;
  59. anim1.fillMode = kCAFillModeForwards;
  60. CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"transform"];
  61. anim2.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
  62. anim2.toValue = [NSValue valueWithCATransform3D:trans2];
  63. anim2.duration = self.moveDuration;
  64. anim2.removedOnCompletion = NO;
  65. anim2.fillMode = kCAFillModeForwards;
  66. self.animGroup.animations = @[anim1, anim2];
  67. [self.imageView.layer addAnimation:self.animGroup forKey:GROUP_ANIM_IDENTIFY];
  68. }else if (anim == groupAnim && flag){
  69. [self.imageView removeFromSuperview];
  70. [self.imageView.layer removeAllAnimations];
  71. if (self.finishBlock){
  72. self.finishBlock();
  73. }
  74. }else if (!flag){
  75. // 如果动画没有执行完成
  76. [self.imageView removeFromSuperview];
  77. [self.imageView.layer removeAllAnimations];
  78. }
  79. }
  80. #pragma mark - property
  81. - (UIImageView *)imageView{
  82. if (_imageView == nil) {
  83. _imageView = [[UIImageView alloc]init];
  84. _imageView.bounds = CGRectMake(0, 0, self.margin, self.margin);
  85. _imageView.layer.cornerRadius = self.margin / 2.0f;
  86. _imageView.layer.masksToBounds = YES;
  87. _imageView.backgroundColor = [UIColor whiteColor];
  88. }
  89. return _imageView;
  90. }
  91. - (CABasicAnimation *)scaleAnim{
  92. if (_scaleAnim == nil) {
  93. _scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
  94. _scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  95. _scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(self.scale, self.scale, 1.0f)];
  96. _scaleAnim.duration = self.scaleDuration;
  97. _scaleAnim.delegate = self;
  98. _scaleAnim.removedOnCompletion = NO;
  99. _scaleAnim.fillMode = kCAFillModeForwards;
  100. }
  101. return _scaleAnim;
  102. }
  103. - (CAAnimationGroup *)animGroup{
  104. if (_animGroup == nil) {
  105. _animGroup = [[CAAnimationGroup alloc]init];
  106. _animGroup.duration = self.moveDuration;
  107. _animGroup.removedOnCompletion = NO;
  108. _animGroup.delegate = self;
  109. }
  110. return _animGroup;
  111. }
  112. @end