开发者社区 问答 正文

如何驱动图像沿曲线运动?

我在开发一个电子商务应用。当我向购物车添加项目的时候,我想要创建一个效果:项目的的图片遵循一个弯曲的路径并在购物车选项卡结束。
我怎样才能创建一个类似这样,图片沿曲线运动的动画效果?

原问题:How can I animate the movement of a view or image along a curved path?

展开
收起
a123456678 2016-07-20 17:41:08 2197 分享 版权
1 条回答
写回答
取消 提交回答
  • UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate]; imageViewForAnimation.alpha = 1.0f; CGRect imageFrame = imageViewForAnimation.frame; //frame.origin,动画开始的地方 CGPoint viewOrigin = imageViewForAnimation.frame.origin; viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f; viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;

    imageViewForAnimation.frame = imageFrame; imageViewForAnimation.layer.position = viewOrigin; [self.view addSubview:imageViewForAnimation];

    // 淡出效果 CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]]; fadeOutAnimation.fillMode = kCAFillModeForwards; fadeOutAnimation.removedOnCompletion = NO;

    //设置缩放 CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"]; [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]]; resizeAnimation.fillMode = kCAFillModeForwards; resizeAnimation.removedOnCompletion = NO;

    // 设置路径运动 CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; pathAnimation.calculationMode = kCAAnimationPaced; pathAnimation.fillMode = kCAFillModeForwards; pathAnimation.removedOnCompletion = NO; //设置动画结束点 CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f); //在最后一个选项卡结束动画 //CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f); CGMutablePathRef curvedPath = CGPathCreateMutable(); CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y); CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y); pathAnimation.path = curvedPath; CGPathRelease(curvedPath);

    CAAnimationGroup *group = [CAAnimationGroup animation]; group.fillMode = kCAFillModeForwards; group.removedOnCompletion = NO; [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]]; group.duration = 0.7f; group.delegate = self; [group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"];

    [imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];

    [imageViewForAnimation release];

    2019-10-17 11:20:52
    赞同 展开评论
问答地址: