123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // FKCollectAnimationTool.m
- // FirstLink
- //
- // Created by jack on 15/9/10.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "FKCollectAnimationTool.h"
- static NSString *COLLECT_ANIMATION_KEY = @"COLLECT_ANIMATION_KEY";
- @implementation FKCollectAnimationTool
- - (void)showAnimationWithCollect:(BOOL)collect inView:(UIView *)view{
-
- self.collectSelected = collect;
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
-
- if (collect) {
- UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"heart"]];
- [window.layer addSublayer:imageView.layer];
- imageView.layer.position = window.layer.position;
- imageView.layer.name = ANIMATION_LAYER_NAME;
-
- CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
- CATransform3D transform1 = CATransform3DIdentity;
- CATransform3D transform2 = CATransform3DMakeScale(1.3f, 1.3f, 1.0f);
- CATransform3D transform3 = CATransform3DIdentity;
- anim.values = @[[NSValue valueWithCATransform3D:transform1], [NSValue valueWithCATransform3D:transform2], [NSValue valueWithCATransform3D:transform3]];
- anim.removedOnCompletion = NO;
- anim.fillMode = kCAFillModeForwards;
-
-
- CAKeyframeAnimation *alphaAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
- alphaAnim.values = @[@0.8, @1.0, @0];
- alphaAnim.removedOnCompletion = NO;
- alphaAnim.fillMode = kCAFillModeForwards;
-
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.animations = @[anim, alphaAnim];
- group.duration = 0.7f;
- group.removedOnCompletion = NO;
- group.fillMode = kCAFillModeForwards;
- group.delegate = self;
- [imageView.layer addAnimation:group forKey:COLLECT_ANIMATION_KEY];
-
- }else{
- UIImageView *leftImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftHeart"]];
- UIImageView *rightImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rightHeart"]];
- [window.layer addSublayer:leftImgView.layer];
- [window.layer addSublayer:rightImgView.layer];
-
- leftImgView.layer.name = ANIMATION_LAYER_NAME;
- rightImgView.layer.name = ANIMATION_LAYER_NAME;
-
- CGFloat leftH = leftImgView.intrinsicContentSize.height;
- CGFloat rightH = rightImgView.intrinsicContentSize.height;
-
- leftImgView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);
- rightImgView.layer.anchorPoint = CGPointMake(0, 1.0f);
-
- leftImgView.layer.position = CGPointMake(window.layer.position.x, window.layer.position.y + leftH / 2);
- rightImgView.layer.position = CGPointMake(window.layer.position.x, window.layer.position.y + rightH / 2);
-
-
- CABasicAnimation *leftRotaAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
- leftRotaAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- leftRotaAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(- M_PI_4, 0, 0, 1.0)];
- leftRotaAnim.removedOnCompletion = NO;
- leftRotaAnim.fillMode = kCAFillModeForwards;
-
- CABasicAnimation *leftAlphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
- leftAlphaAnim.fromValue = @1.0;
- leftAlphaAnim.toValue = @0;
- leftAlphaAnim.removedOnCompletion = NO;
- leftAlphaAnim.fillMode = kCAFillModeForwards;
-
- CAAnimationGroup *leftGroup = [CAAnimationGroup animation];
- leftGroup.animations = @[leftRotaAnim, leftAlphaAnim];
- leftGroup.duration = 0.5f;
- leftGroup.removedOnCompletion = NO;
- leftGroup.fillMode = kCAFillModeForwards;
- leftGroup.delegate = self;
-
- CABasicAnimation *rightRotaAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
- rightRotaAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- rightRotaAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1.0)];
- rightRotaAnim.removedOnCompletion = NO;
- rightRotaAnim.fillMode = kCAFillModeForwards;
-
- CABasicAnimation *rightAlphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
- rightAlphaAnim.fromValue = @1.0;
- rightAlphaAnim.toValue = @0;
- rightAlphaAnim.removedOnCompletion = NO;
- rightAlphaAnim.fillMode = kCAFillModeForwards;
-
- CAAnimationGroup *rightGroup = [CAAnimationGroup animation];
- rightGroup.animations = @[rightRotaAnim, rightAlphaAnim];
- rightGroup.duration = 0.5f;
- rightGroup.removedOnCompletion = NO;
- rightGroup.fillMode = kCAFillModeForwards;
- rightGroup.delegate = self;
-
- [leftImgView.layer addAnimation:leftGroup forKey:COLLECT_ANIMATION_KEY];
- [rightImgView.layer addAnimation:rightGroup forKey:COLLECT_ANIMATION_KEY];
- }
- }
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
-
- CALayer *targetLayer = nil;
- for (CALayer *layer in window.layer.sublayers) {
- if ([layer animationForKey:COLLECT_ANIMATION_KEY] == anim){
- targetLayer = layer;
- break;
- }
- }
-
- if (targetLayer) [targetLayer removeFromSuperlayer];
-
- BOOL finished = YES;
- for (CALayer *layer in window.layer.sublayers) {
- if ([layer.name isEqualToString:ANIMATION_LAYER_NAME]) {
- finished = NO;
- break;
- }
- }
-
- // window 上没有动画了,执行delegate
- if (finished) {
- if ([self.delegate respondsToSelector:@selector(allCollectAnimationFinish:)]){
- [self.delegate allCollectAnimationFinish:self];
- }
- }
- }
- @end
|