gpt4 book ai didi

iphone - 让图像遵循 iPhone 中预定义的路径?

转载 作者:行者123 更新时间:2023-12-03 20:48:23 25 4
gpt4 key购买 nike

我正在寻找某种方法让图像(球)沿着 iPhone 中的预定义路径移动。我的目的是创造一种类似于迷宫运动的球运动。我知道有一种方法可以使用 CGPath 以编程方式创建路径。但我相信创建复杂的路径是很困难的。 是否有更好、更简单的方法来从图像创建路径(看起来或代表路径)并使图像(球)运动限制在该路径中?

提前感谢帮助...

最佳答案

创建沿路径移动对象的动画并不难。例如,以下代码将沿着特定的贝塞尔曲线制作动画:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 1.0f;
pathAnimation.calculationMode = kCAAnimationPaced;

CGPoint currentPosition = viewToAnimate.layer.position;
CGPoint endPoint = CGPointMake(currentPosition.x + 100.0f, currentPosition.y - 50.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, currentPosition.x, currentPosition.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, currentPosition.y, endPoint.x, currentPosition.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
[viewToAnimate.layer addAnimation:pathAnimation forKey:@"animateMovementUsingPath"];

该代码的中心部分是定义路径的地方。在本例中,我从 currentPosition 开始绘制,然后添加一条以 endPoint 结束的曲线。该曲线的控制点为 (endPoint.x, currentPosition.y)(endPoint.x, currentPosition.y)

以这种方式定义矢量曲线并让 Core Animation 为您处理所有补间比您自己管理所有动画要容易得多。

关于iphone - 让图像遵循 iPhone 中预定义的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3559285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com