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);

    });

}

猜你喜欢

转载自blog.csdn.net/sinat_23907467/article/details/78626928