为 UIImageView 创建边界框

发布于 2024-10-22 01:12:11 字数 152 浏览 1 评论 0原文

如何为不是 CGRect 的 UIImageView 创建边界框?

我希望视图中的对象能够显示图像并检测碰撞。

问题是我希望这些对象具有任何形状,而不是将它们安装到 CGRect 中并检测框内但不是实际图像的区域上的碰撞。

如何实现这一目标?

How can a bounding box be created for a UIImageView that is not a CGRect?

I would like to have objects in my view which should display images as well as detection collisions.

The issue is I would like these object to be whatever shape they are rather than fitting them into a CGRect and detecting collisions on areas which are inside the box but are nit the actual image.

How does one achieve this?

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

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

发布评论

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

评论(1

戏剧牡丹亭 2024-10-29 01:12:11

这是一个不平凡的问题。但基本原理是 CGRect 是一个矩形,并且矩形内部的命中测试相当容易理解。但是,听起来您想要更复杂的形状。 UIImageView 显示图像。它不知道您想要使用什么形状进行碰撞测试。所以你必须告诉它。

一件简单的事情就是查看显示图像的 Alpha/透明度值来创建形状。因此,要回答一个点击中图像的问题,我们计算出该点在图像中的位置,如果 alpha 大于 0,则返回 true。如果这样做,您可以创建任何具有透明背景的图像,代码将工作。

如果这对您不起作用,那么还可以对点和多边形运行命中测试,这篇文章详细介绍了这一点。

如何确定 2D 点是否在多边形内?

This is a non-trivial problem. But the basics are a CGRect is a rectangle and a hit test inside of a rectangle is fairly easy to understand. However, you sound like you want a more complex shape. UIImageView displays an image. It does not have any idea about what shape you want to use for your collision test. So you are going to have to tell it.

One easy thing to do is to look at the alpha/transparent values of the display image to create a shape. So to answer the question is a point hitting an image we figure out the location of point in the image and return true if the alpha is greater than 0. If you do this you can create any image with a transparent background and the code will just work.

If that will not work for you then can can also run a hit test on a point and a polygon this post covers that in detail.

How can I determine whether a 2D Point is within a Polygon?

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