@interface ShopViewController ()
{
CALayer *_goodsImageLayer;
UIBezierPath *_goodsImagePath;
}
...
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID =@"myid";
ShopTableViewCell * cell =nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil)
{
cell = [[ShopTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];
cell.contentView.backgroundColor=[UIColorwhiteColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.delegate=self;
}
... 传数据...
//cell.model = ...
//添加购物车按钮的block回调
cell.ShopTableViewCellBlock = ^(UIImageView *imageView){
CGRect rect = [tableViewrectForRowAtIndexPath:indexPath];//获取cell的位置
rect.origin.y = rect.origin.y - self.shopTableView.contentOffset.y;
CGRect imageRect =CGRectMake(CGRectGetMidX(imageView.frame) -15, CGRectGetMinY(imageView.frame),30, 30);//动画小图的位置,自定义的
imageRect.origin.y = imageRect.origin.y + rect.origin.y;
[selfstartAnimatingWithRect:imageRectImageView:imageView];
};
return cell;
}
#pragma add to shop cart Animating
- (void) startAnimatingWithRect:(CGRect)imageRect ImageView:(UIImageView *)imageView{
_goodsImageLayer = [CALayerlayer];
_goodsImageLayer.contents = imageView.layer.contents;
_goodsImageLayer.bounds = imageRect;
_goodsImageLayer.contentsGravity =kCAGravityResizeAspectFill;
_goodsImageLayer.position =CGPointMake(CGRectGetMidX(imageView.frame),CGRectGetMidY(imageRect));
[self.view.layeraddSublayer:_goodsImageLayer];
//贝塞尔曲线实现抛物线动画
_goodsImagePath = [UIBezierPathbezierPath];
//设置起点坐标
[_goodsImagePathmoveToPoint:_goodsImageLayer.position];
//画抛物线,结束位置和抛物线顶底位置,自定义的
[_goodsImagePathaddQuadCurveToPoint:CGPointMake(kScreenWidth*0.70,kScreenHeight -40 - 64) controlPoint:CGPointMake(CGRectGetMaxX(imageView.frame) + 30,imageRect.origin.y -CGRectGetMaxY(imageView.frame))];
[self.layerArrayaddObject:_goodsImageLayer];
[selfgroupAnimation];
}
- (void) groupAnimation{
//移动
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
pathAnimation.path =_goodsImagePath.CGPath;
pathAnimation.rotationMode =kCAAnimationRotateAuto;
//翻转
CABasicAnimation *rotationAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation"];
rotationAnimation.toValue = @M_PI;
rotationAnimation.duration =1.0f;
//缩放
CABasicAnimation *narrowAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
narrowAnimation.toValue =@0.3;
narrowAnimation.beginTime =0.5;
narrowAnimation.duration =0.5f;
narrowAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];
//组动画
CAAnimationGroup *groupAnimation = [CAAnimationGroupanimation];
groupAnimation.animations =@[pathAnimation, rotationAnimation, narrowAnimation];
groupAnimation.duration =1.0f;
groupAnimation.fillMode =kCAFillModeForwards;
groupAnimation.removedOnCompletion =NO;
groupAnimation.delegate =self;
[_goodsImageLayeraddAnimation:groupAnimationforKey:@"group"];//添加动画
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//动画结束,处理因多次添加商品,引起_goodsImageLayer没有移除的bug
if (anim == [_goodsImageLayeranimationForKey:@"group"]) {
[self.layerArrayenumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx,BOOL * _Nonnull stop) {
[obj removeFromSuperlayer];
}];
}
}
还可以增加其他处理,比如图片圆角处理,比如抛物线路径,比如添加一些其他的动画,如果后面有时间,会添加到Github上。