UIView+LikeAnimation.m 3.26 KB
//
//  UIView+LikeAnimation.m
//  test
//
//  Created by wangyang on 16/9/21.
//  Copyright © 2016年 北京更美互动信息科技有限公司. All rights reserved.
//

#import "UIView+LikeAnimation.h"

@implementation UIView (LikeAnimation)

- (void)addLikeAnimationWithFrame:(CGRect)frame {
    UIImageView *likeView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topic_zan_pink"]];
    likeView.frame = frame;
    [likeView addLikeAnimationFrom:likeView.center];
    [self addSubview:likeView];
}

- (void)addLikeAnimationFrom:(CGPoint)point {

    CGFloat totalTime = 1;

    // 放大
    CABasicAnimation *scaleBig = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleBig.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    scaleBig.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.5, 1.0)];
    scaleBig.removedOnCompletion = YES;
    scaleBig.beginTime = 0;
    scaleBig.duration = 0.5 * totalTime;
    scaleBig.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    // 缩小
    CABasicAnimation *scaleSmall = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleSmall.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.5, 1.0)];
    scaleSmall.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)];
    scaleSmall.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    scaleSmall.removedOnCompletion = YES;
    scaleSmall.beginTime =  0.5 * totalTime;
    scaleSmall.duration = 0.5 * totalTime;

    // 渐隐
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.fromValue = @1;
    alphaAnimation.toValue = @0.25;
    alphaAnimation.removedOnCompletion = YES;
    alphaAnimation.beginTime =  0.5 * totalTime;
    alphaAnimation.duration = 0.5 * totalTime;

    // 旋转
    CABasicAnimation *rotate =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotate.fromValue = [NSNumber numberWithFloat:0]; // 开始时的角度
    rotate.toValue = [NSNumber numberWithFloat:M_PI_4]; // 结束时的角度
    rotate.beginTime = 0;
    rotate.duration = 0.5 * totalTime;
    rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotate.removedOnCompletion = NO;
    rotate.fillMode = kCAFillModeForwards;

    // 路径
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:point];
    [movePath addQuadCurveToPoint:CGPointMake(point.x + 35, point.y - 43) controlPoint:CGPointMake(point.x + 27, point.y - 115)];
    CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    posAnim.path = movePath.CGPath;
    posAnim.removedOnCompletion = YES;

    // 组合动画
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObjects:scaleBig, scaleSmall, posAnim, rotate, alphaAnimation, nil];
    group.duration = 1 * totalTime;
    group.autoreverses = NO;
    group.repeatCount = 1;
    
    [self.layer addAnimation:group forKey:nil];

    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1 * totalTime inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
@end