iPhone 中的运动路径/运动指南?

发布于 2024-08-12 11:41:35 字数 1382 浏览 2 评论 0原文

我想要一个图像根据编程代码绘制的路径移动。

我问过类似的问题(https:// /stackoverflow.com/questions/1571182/how-to-limit-a-uiimageview-to-move-in-a-specific-path)之前,但我发现我问错了。该图像不必是 UIImageView。另外,后来发现上面给出的答案仅适用于Mac OS X,不适用于iPhone。

无论如何,这是我绘制简单路径的代码。

CGContextSetRGBStrokeColor(theContext, 1.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(theContext, 2.0);

// Draw a bezier curve with end points s,e and control points cp1,cp2
CGPoint s = CGPointMake(30.0, 120.0);
CGPoint e = CGPointMake(300.0, 120.0);
CGPoint cp1 = CGPointMake(120.0, 30.0);
CGPoint cp2 = CGPointMake(210.0, 210.0);

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, s.x, s.y);
CGPathAddCurveToPoint(path, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
CGPathAddCurveToPoint(path, NULL, cp2.x, cp1.y, cp1.x, cp2.y, s.x, s.y);
CGPathCloseSubpath(path);

CGContextSetRGBStrokeColor(theContext, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextAddPath(theContext, self.path);
CGContextSetLineWidth(theContext, 1.0);
CGContextStrokePath(theContext);

其中 theContext 是 CGContextRef。上述代码的结果将绘制一个旋转90度的“8”形状。但是,如何让图像跟随路径呢?

更新#1:抱歉重复问题,但我想进一步询问如何根据路径使图像旋转(即头向量应遵循路径的切线)。谁能提出一些想法?

更新#2:如何使路径动态变化?

I want an image to move according to the path drawn by programming codes.

I have asked a similar question (https://stackoverflow.com/questions/1571182/how-to-limit-a-uiimageview-to-move-in-a-specific-path) before, but I found that I asked in a wrong way. The image needs not to be a UIImageView. Also, the answer given above was later found that it is for Mac OS X only, not for iPhone.

Anyway, here is my codes for drawing a simple path.

CGContextSetRGBStrokeColor(theContext, 1.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(theContext, 2.0);

// Draw a bezier curve with end points s,e and control points cp1,cp2
CGPoint s = CGPointMake(30.0, 120.0);
CGPoint e = CGPointMake(300.0, 120.0);
CGPoint cp1 = CGPointMake(120.0, 30.0);
CGPoint cp2 = CGPointMake(210.0, 210.0);

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, s.x, s.y);
CGPathAddCurveToPoint(path, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
CGPathAddCurveToPoint(path, NULL, cp2.x, cp1.y, cp1.x, cp2.y, s.x, s.y);
CGPathCloseSubpath(path);

CGContextSetRGBStrokeColor(theContext, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextAddPath(theContext, self.path);
CGContextSetLineWidth(theContext, 1.0);
CGContextStrokePath(theContext);

where theContext is CGContextRef. the result of above codes will draw a "8" shape rotated by 90 degree. But, how to make an image follow the path?

UPDATE #1: Sorry for duplicating questions, but I would like to further ask about how to make the image's rotation according to the path (i.e. the head vector should follow the tangent of the path) . Can anyone suggests some ideas?

UPDATE #2: How to make the path changing dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-08-19 11:41:35

您在其他帖子中得到的答案适用于您的情况。您必须自己完成从 AppKit 到 UIKit 的转换。

The answer you got to your other post applies to your situation. You'll have to do the translation from AppKit to UIKit yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文