加入购物车的粒子动画

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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 3,071评论 0 21
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,578评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,147评论 5 13
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,484评论 2 4
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,169评论 1 23