如何沿着贝塞尔曲线拖动UIImageView?

6
我正在开发一款游戏,其中一个 UIImageView 沿着一条 Bezier 路径移动。我希望用户可以通过触摸和拖拽 UIImageView 在路径上进行操作!那么如何实现呢?请参考下方的示意图:

enter image description here

3个回答

0
请尝试以下内容...
这里的selectedImageCat是全局uiimageview对象,touchimgView是选择(触摸)的imageview。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        for (UITouch *touchPoint in touches)
        {
              if(CGRectContainsPoint(touchimgView.frame, [touchPoint locationInView:self.view]))
        selectedImageCat=(UIImageView*)touchimgView;

        }
    }

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (selectedImageCat!=nil)
        {
            CGPoint pt = [[touches anyObject] locationInView:self.view];
            [selectedImageCat setCenter:pt];
        }
    }

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {


        //here you provide final frame for touchimgView object
    }

已经完成了,它是用于在屏幕上移动图像而不是选择路径的。 - Manjit Singh

0

只需使用平移手势并将其添加到UIImageView中,并提供用户移动它的路径。平移手势

这个链接可能有助于理解和实现相同的功能。如果还有其他问题,请告诉我。


对于那个贝塞尔路径,你应该将其提供给用户作为一条大线/绳索,以便他/她知道沿着哪条路径移动图像。 - nikhil84
我看到那个链接,该链接只是用来移动屏幕上的图片。我希望当用户触摸图片时,图片可以拖动并沿预定义路径流动。 - Manjit Singh
当检测到平移手势并调用函数时,您可以检查路径并根据自己的逻辑移动图像或不移动。 - nikhil84
你能提供一些关于你的游戏的想法吗? - nikhil84
所以每当点匹配时,将该点放入前一个点中并检查图像的下一个点。 - nikhil84
显示剩余14条评论

0

你必须检查触摸点坐标是否在贝塞尔路径内。如果在内部,则允许UITouch和Movement,否则不允许。相应地给出If条件


请问您能否提供一些代码或文档,告诉我如何实现这个功能? - Manjit Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接