加入购物车动画

@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上。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值