加入购物车的粒子动画

作者: iOS开发技术 | 来源:发表于2016-11-14 11:28 被阅读296次

Demo 下载

111111.gif

调用方式为直接调用CALayer的静态方法

[CALayer startAnimatAtBeginView:btn endView:weakself.cartView finished:nil];

/**
 *  加入购物车的粒子动画
 *
 *  @param begiView 开始地方的View
 *  @param endView  结束地方的View
 *  @param finished 动画结束回调
 */
+ (void)startAnimatAtBeginView:(UIView *)begiView
                       endView:(UIView *)endView
                      finished:(void(^)(void))finished;

内部实现

+ (void)startAnimatAtBeginView:(UIView *)begiView endView:(UIView *)endView finished:(void(^)(void))finished
{
   
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //获取动画起始点和结束点
    CGPoint startPoint = [begiView convertRect: begiView.bounds toView:window].origin;
    CGPoint endPoint = [endView convertRect: endView.bounds toView:window].origin;
    endPoint.x = endPoint.x + endView.bounds.size.width*0.5;
    endPoint.y = endPoint.y + endView.bounds.size.height*0.5;

    //设置粒子
    CALayer *dotLayer = [CALayer layer];
    dotLayer.backgroundColor = [UIColor redColor].CGColor;
    dotLayer.frame = CGRectMake(0, 0, 15, 15);
    dotLayer.cornerRadius = 15 /2;
    [window.layer addSublayer:dotLayer];
    
    dotLayer.endView = endView;
    dotLayer.finished = finished;
    
    //设置贝塞尔曲线
    UIBezierPath *path= [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    //三点曲线,设置路径
    [path addCurveToPoint:endPoint
            controlPoint1:startPoint
            controlPoint2:CGPointMake(startPoint.x - 180, startPoint.y - 150)];
    
    
    //设置动画组
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = 0.1f;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = 0.5f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = dotLayer;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [dotLayer addAnimation:groups forKey:nil];
}

粒子动画结束

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
        !self.finished? :self.finished();
    
// 设置购物车的抖动动画
        CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        shakeAnimation.duration = 0.1f;
        shakeAnimation.fromValue = [NSNumber numberWithFloat:0.7];
        shakeAnimation.toValue = [NSNumber numberWithFloat:1];
        shakeAnimation.autoreverses = YES;
        [self.endView.layer addAnimation:shakeAnimation forKey:nil];
        
        [self removeFromSuperlayer];
    }
}

相关文章

网友评论

    本文标题:加入购物车的粒子动画

    本文链接:https://www.haomeiwen.com/subject/nidhpttx.html