购物车动画-贝塞尔曲线


购物车动画


#pragma mark - 购物车动画
- (void)startAnimation:(CGPoint)center indexPath:(NSIndexPath *)indexPath
{
    
    /*
     1.构造点
     */
    //起点
    CGPoint startPoint = center;
    //终点
    CGPoint endPoint = self.shoppingCarButton.center;
    //控制点
    CGPoint controlPoint = CGPointMake(endPoint.x, startPoint.y);
    
    
    
    /*
     2.构造一个path
     */
    //构造一个空的path,C语言的方法里面如果有Create,copy,retain,allock都需要手动管理
    CGMutablePathRef path = CGPathCreateMutable();
    //添加起点
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    //添加贝塞尔曲线
    //CGPathAddQuadCurveToPoint(path,反射变换(一般传NULL),控制点x, 控制点y,终点x, 终点y)
    CGPathAddQuadCurveToPoint(path, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
    
    /*
     3.创建执行动画的对象
     */
    Goods *goods = self.goodsArray[indexPath.row];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:goods.imageName]];
    imageView.bounds = CGRectMake(0, 0, 40, 40);
    imageView.center = startPoint;
    imageView.layer.cornerRadius = 20;
    imageView.clipsToBounds = YES;
    [self.view addSubview:imageView];
    
    //添加到数组中
    [self.animationImageViewArray addObject:imageView];
    
    
    
    /*
     4.创建关键帧动画,只能添加到层(CALayer)上面.
     */
    //注意:目前可以认为position就是UIView的center.
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //提供关键点,直线运行
    //animation.values;
    //提供路径
    animation.path = path;
    //动画时间
    animation.duration = 1;
    //设置代理
    animation.delegate = self;
    
    //停留终点
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    
    //执行动画
    [imageView.layer addAnimation:animation forKey:nil];
    
    //释放path,arc不会管理CG开始的变量。
    CGPathRelease(path);
}

#pragma mark - 动画完成会触发
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //flag如果是YES表示动画完成,NO表示动画没有执行完别取消了。
    if (flag)
    {
        NSLog(@"动画完成");
        
#if 0
        //注意:当变量可变数组的时候,不能对元素进行添加、删除元素
        for (UIImageView *imageView in self.animationImageViewArray)
        {
            [self.animationImageViewArray removeObject:imageView];
        }
#endif
        
        //1.移除执行动画view
        UIImageView *imageView = [self.animationImageViewArray firstObject];
        //1.1-从数组中把视图视图
        [self.animationImageViewArray removeObject:imageView];
        //1.2-从父视图移除
        [imageView removeFromSuperview];
        
        //2.修改购物车数量
        self.shoppingCarButton.badgeValue = ++_numberOfGoodsInCar;
    }
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值