123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- //
- // FKAddBasketAnimation.m
- // FirstLink
- //
- // Created by jack on 16/5/9.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKAddBasketAnimTool.h"
- static NSString *SCALE_ANIM_IDENTIFY = @"SCALE_ANIM_IDENTIFY";
- static NSString *GROUP_ANIM_IDENTIFY = @"GROUP_ANIM_IDENTIFY";
- static NSString *MOVE_POINT_KEY = @"MOVE_POINT_KEY";
- @interface FKAddBasketAnimTool ()
- @property (nonatomic, strong) UIImageView *imageView;
- @property (nonatomic, strong) CABasicAnimation *scaleAnim;
- @property (nonatomic, strong) CAAnimationGroup *animGroup;
- @property (nonatomic, assign) BOOL removedOnComplition;
- @end
- @implementation FKAddBasketAnimTool
- - (instancetype)init{
- if (self = [super init]) {
- self.scale = 4.0f;
- self.scaleDuration = 0.4f;
- self.moveDuration = 0.4f;
- self.removedOnComplition = YES;
-
- self.margin = 30.0f;
- }
- return self;
- }
- - (void)showInView:(UIView *)inView targetView:(UIView *)targetView{
-
- if (!inView || !targetView) return;
- CGPoint centerPoint = inView.center;
- CGRect targetFrame = [targetView convertRect:targetView.bounds toView:inView];
- CGPoint targetPoint = CGPointMake(CGRectGetMidX(targetFrame), CGRectGetMidY(targetFrame));
- CGPoint moveTarget = CGPointMake(targetPoint.x - centerPoint.x, targetPoint.y - centerPoint.y);
-
- [self.scaleAnim setValue:[NSValue valueWithCGPoint:moveTarget] forKey:MOVE_POINT_KEY];
-
- [inView addSubview:self.imageView];
- self.imageView.center = inView.center;
- if (self.image){
- self.imageView.image = self.image;
- }else if (self.picUrl.length){
- [self.imageView setImageWithURL:self.picUrl cdnWidth:80];
- }
-
- [self.imageView.layer addAnimation:self.scaleAnim forKey:SCALE_ANIM_IDENTIFY];
-
- }
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
-
- // NSString *identify = [[anim valueForKey:ANIMTIOAN_IDENTIFY_KEY] stringValue];
-
- CAAnimation *scaleAnim = [self.imageView.layer animationForKey:SCALE_ANIM_IDENTIFY];
- CAAnimation *groupAnim = [self.imageView.layer animationForKey:GROUP_ANIM_IDENTIFY];
-
- if (anim == scaleAnim && flag){
-
- CGPoint movePoint = [[self.scaleAnim valueForKey:MOVE_POINT_KEY] CGPointValue];
- CATransform3D trans2 = CATransform3DMakeTranslation(movePoint.x, movePoint.y, 0);
-
- //同一keypath只能保留一个animation状态
- self.imageView.layer.transform = CATransform3DMakeScale(self.scale, self.scale, 1.0f);
-
- CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform"];
- anim1.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- anim1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)];
- anim1.duration = self.moveDuration;
- anim1.removedOnCompletion = NO;
- anim1.fillMode = kCAFillModeForwards;
-
- CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"transform"];
- anim2.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- anim2.toValue = [NSValue valueWithCATransform3D:trans2];
- anim2.duration = self.moveDuration;
- anim2.removedOnCompletion = NO;
- anim2.fillMode = kCAFillModeForwards;
- self.animGroup.animations = @[anim1, anim2];
-
- [self.imageView.layer addAnimation:self.animGroup forKey:GROUP_ANIM_IDENTIFY];
- }else if (anim == groupAnim && flag){
- [self.imageView removeFromSuperview];
- [self.imageView.layer removeAllAnimations];
-
- if (self.finishBlock){
- self.finishBlock();
- }
- }else if (!flag){
- // 如果动画没有执行完成
- [self.imageView removeFromSuperview];
- [self.imageView.layer removeAllAnimations];
- }
- }
- #pragma mark - property
- - (UIImageView *)imageView{
- if (_imageView == nil) {
- _imageView = [[UIImageView alloc]init];
- _imageView.bounds = CGRectMake(0, 0, self.margin, self.margin);
- _imageView.layer.cornerRadius = self.margin / 2.0f;
- _imageView.layer.masksToBounds = YES;
- _imageView.backgroundColor = [UIColor whiteColor];
- }
- return _imageView;
- }
- - (CABasicAnimation *)scaleAnim{
- if (_scaleAnim == nil) {
- _scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
- _scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- _scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(self.scale, self.scale, 1.0f)];
- _scaleAnim.duration = self.scaleDuration;
- _scaleAnim.delegate = self;
- _scaleAnim.removedOnCompletion = NO;
- _scaleAnim.fillMode = kCAFillModeForwards;
- }
- return _scaleAnim;
- }
- - (CAAnimationGroup *)animGroup{
- if (_animGroup == nil) {
- _animGroup = [[CAAnimationGroup alloc]init];
- _animGroup.duration = self.moveDuration;
- _animGroup.removedOnCompletion = NO;
- _animGroup.delegate = self;
- }
- return _animGroup;
- }
- @end
|