iPhone 中的运动路径/运动指南?
我想要一个图像根据编程代码绘制的路径移动。
我问过类似的问题(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在其他帖子中得到的答案适用于您的情况。您必须自己完成从 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.