iOS 添加到购物车 小动画



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    int y = 1 + (arc4random() % 8);

    int width = 20 +  (arc4random() % 100);

    NSInteger x = 0 + (arc4random() % (NSInteger)(self.view.frame.size.width));

    [selfaddToShoppingCartWithGoodsImage:[UIImageimageNamed:[NSString stringWithFormat:@"%d",y]]startPoint:CGPointMake(self.view.center.x,self.view.frame.origin.y)endPoint:CGPointMake(x,self.view.frame.size.height)AndAnimalWidth:width completion:^(BOOL isFish) {

        //动画完成

    }];

}

-(void)addToShoppingCartWithGoodsImage:(UIImage *)goodsImage startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint AndAnimalWidth:(int)width completion:(void (^)(BOOL))completion{

    

    //------- 创建shapeLayer -------//

    CAShapeLayer *animationLayer = [[CAShapeLayeralloc] init];

    animationLayer.frame = CGRectMake(startPoint.x - 20, startPoint.y -20, width, width);

    animationLayer.contents = (id)goodsImage.CGImage;

    

    // 获取window的最顶层视图控制器

    UIViewController *rootVC = [[UIApplicationsharedApplication].delegate window].rootViewController;

    UIViewController *parentVC = rootVC;

    while ((parentVC = rootVC.presentedViewController) !=nil ) {

        rootVC = parentVC;

    }

    while ([rootVCisKindOfClass:[UINavigationControllerclass]]) {

        rootVC = [(UINavigationController *)rootVCtopViewController];

    }

    

    // 添加layer到顶层视图控制器上

    [rootVC.view.layeraddSublayer:animationLayer];

    

    

    //------- 创建移动轨迹 -------//

    UIBezierPath *movePath = [UIBezierPathbezierPath];

    [movePath moveToPoint:startPoint];

    [movePath addQuadCurveToPoint:endPointcontrolPoint:CGPointMake(200,100)];

    // 轨迹动画

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

    CGFloat durationTime = 1; // 动画时间1秒

    pathAnimation.duration = durationTime;

    pathAnimation.removedOnCompletion =NO;

    pathAnimation.fillMode =kCAFillModeForwards;

    pathAnimation.path = movePath.CGPath;

    

    

    //------- 创建缩小动画 -------//

    CABasicAnimation *scaleAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

    scaleAnimation.fromValue = [NSNumbernumberWithFloat:1.0];

    if (width > 100) {

        scaleAnimation.toValue = [NSNumbernumberWithFloat:0.2];

    }else{

        scaleAnimation.toValue = [NSNumbernumberWithFloat:0.5];

    }

    scaleAnimation.duration = 1.0;

    scaleAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    scaleAnimation.removedOnCompletion =NO;

    scaleAnimation.fillMode =kCAFillModeForwards;

    

    

    // 添加轨迹动画

    [animationLayer addAnimation:pathAnimationforKey:nil];

    // 添加缩小动画

    [animationLayer addAnimation:scaleAnimationforKey:nil];

    

    

    //------- 动画结束后执行 -------//

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(durationTime *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [animationLayer removeFromSuperlayer];

        completion(YES);

    });

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值