如何根据用户拖动视图沿弯曲的 CGPath 移动 UIView
我正在尝试构建一个界面,用户可以在屏幕上移动手指,图像列表沿着路径移动。这个想法是图像中心永远不会离开路径。
我发现的大部分内容都是关于如何使用 CGPath 制作动画,而不是实际使用路径作为用户移动的轨迹。
== 更新。更好的描述==
即使用户没有在路径上移动手指,我也需要在路径上跟踪对象。
例如(下图),如果对象位于路径的开头,并且用户触摸屏幕上的任意位置并从左向右移动手指,我需要对象从左向右移动但遵循路径,即,当它向右移动到路径末端时向上。
==结束更新
这是我绘制的路径,想象一下我将有一个视图(任何图像),用户可以触摸它并沿着路径拖动它,不需要将手指精确地移动到路径上。如果用户从左向右移动,图像应该从左向右移动,但如果需要沿着路径移动,则图像应该向上移动。
这就是我创建路径的方式:
CGPoint endPointUp = CGPointMake(315, 124);
CGPoint endPointDown = CGPointMake(0, 403);
CGPoint controlPoint1 = CGPointMake(133, 187);
CGPoint controlPoint2 = CGPointMake(174, 318);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, endPointUp.x, endPointUp.y);
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPointDown.x, endPointDown.y);
知道如何实现此目的吗?
I'm trying to build a interface that the user can move his finger around the screen an a list of images moves along a path. The idea is that the images center nevers leaves de path.
Most of the things I found was about how to animate using CGPath and not about actually using the path as the track to a user movement.
== Update. Better description ==
I need to objects to be tracked on the path even if the user isn't moving his fingers over the path.
For example (image bellow), if the object is at the beginning of the path and the user touches anywhere on the screen and moves his fingers from left to right I need that the object moves from left to right but following the path, that is, going up as it goes to the right towards the path's end.
== End update
This is the path I've draw, imagine that I'll have a view (any image) that the user can touch and drag it along the path, there's no need to move the finger exactly over the path. If the user move from left to right the image should move from left to right but going up if need following the path.
This is how I'm creating the path:
CGPoint endPointUp = CGPointMake(315, 124);
CGPoint endPointDown = CGPointMake(0, 403);
CGPoint controlPoint1 = CGPointMake(133, 187);
CGPoint controlPoint2 = CGPointMake(174, 318);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, endPointUp.x, endPointUp.y);
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPointDown.x, endPointDown.y);
Any idead how can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以采用绘制图形的数学函数,然后沿着该轨迹
对象移动。
You can take a mathematical function that draws a graph, and then move along this trajectory
object.