CAKeyframeAnimation CABasicAnimation 结合做加入购物车的动画

开始动画

  • 创建一个Calyer的全局变量,
 CALayer  *_layer;
  • 设置动画过程(注意动画的过程是移动一个ImageView)
//添加加入购物车动画
- (void)addAnimationPath:(HSGoodsListTableViewCell *)cell
{
    HSGoodsListTableViewCell *bolthCell=(HSGoodsListTableViewCell *)cell;
    _layer = [CALayer layer];
    _layer.contentsGravity = kCAGravityResizeAspectFill;
    UIImageView *imagV = bolthCell.goodsImage;
    _layer.contents = (id)imagV.layer.contents;
    CGRect frame = [bolthCell.goodsImage convertRect:bolthCell.goodsImage.frame toView:self.view];
    _layer.bounds=frame;
    
    CGPoint p=[bolthCell.goodsImage convertPoint:bolthCell.goodsImage.center toView:self.view];
    
    //创建贝塞尔曲线
    UIBezierPath *bPath=[UIBezierPath bezierPath];
    [bPath moveToPoint:p];
    [bPath addQuadCurveToPoint:CGPointMake(shopCarBtnCenter.x,shopCarBtnCenter.y) controlPoint:CGPointMake(shopCarBtnCenter.x, p.y)];
    [self groupAnimation:bPath];
    [self.view.layer addSublayer:_layer];
    
}

//构建一组动画
-(void)groupAnimation:(UIBezierPath *)path
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    //动画缩小
    CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    narrowAnimation.beginTime = 0;
    narrowAnimation.fromValue = [NSNumber numberWithFloat:0.6f];
    narrowAnimation.duration = 0.5f;
    narrowAnimation.toValue = [NSNumber numberWithFloat:0.1f];
    narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //动画组建
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,narrowAnimation];
    groups.duration = 0.5f;
    groups.removedOnCompletion=YES;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    //根据路线画路线或填充
    [_layer addAnimation:groups forKey:nil];
}
//移除动画
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [_layer removeFromSuperlayer];
}

你可能感兴趣的:(CAKeyframeAnimation CABasicAnimation 结合做加入购物车的动画)