购物车动画
#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;
}
}