使用UIView的convertRect方法多次检测CGRectIntersectsRect的进阶方法

发布于 2024-08-26 20:52:59 字数 911 浏览 4 评论 0原文

我最近问了一个问题子视图内的碰撞检测,有一个完美的答案。我已经到了在应用程序上实现碰撞的最后一点,但我遇到了一个新问题。

使用convertRect可以很好地从子视图中获取CGRect。我需要它更复杂一点,因为它并不完全是需要检测的矩形。

在 XCode 上,我创建了一个名为 TileViewController 的抽象类。除其他属性外,它还有一个 IBOutlet UIView *detectionView; 我现在有多个继承自 TileViewController 的类,每个类都有多个嵌套在我使用 Interface Builder 创建的 detectorView 内的视图。

这个想法是一个对象可以具有特定的形状或大小,我以编程方式将这些“平铺”检测点放置在每个对象的底部中心。用户可以选择一个项目并与其交互,在这种情况下可以移动它。

现在该方法本身在一定程度上可以工作,但我认为由于检测关闭,它无法正确处理嵌套值。

此方法的简化版本有效 - 在 detectorView 本身上使用 CGRectIntersectsRect,因此我我想知道我是否正确地循环并检查了视图?

我不确定它是否在同一个视图中进行比较,但我怀疑是的,我确实在某一点上稍微修改了代码,而不是比较 self.view 中的值,我将 viewController.detectView 的 UIViews 放入 InteractiveView.detectView 中但结果是一样的。

它是经过操纵的,因此子视图会改变颜色,但是当它们甚至没有触摸时它们就会改变颜色,并且当它们触摸错误的 UIview 时也会改变颜色

I recently asked a question regarding collision detection within subviews, with a perfect answer. I've come to the last point in implementing the collision on my application but I've come across a new issue.

Using convertRect was fine getting the CGRect from the subView. I needed it to be a little more complex as it wasn't exactly rectangles that needed to be detected.

on XCode I created an abstract class called TileViewController. Amongst other properties it has a IBOutlet UIView *detectionView;
I now have multiple classes that inherit from TileViewController, and each class there are multiple views nested inside the detectionView which I have created using Interface Builder.

The idea is an object could be a certain shape or size, I've programatically placed these 'tiled' detection points bottom center of each object. A user can select an item and interactive with it, in this circumstance move it around.

Now the method itself works to an extent but I don't think it's working with the nested values properly as the detection is off.

A simplified version of this method works - Using CGRectIntersectsRect on the detectionView itself so I'm wondering if I'm looping through and checking the views correctly?

I wasn't sure whether it was comparing in the same view but I suspect it is, I did modify the code slightly at one point, rather then comparing the values in self.view I took the viewController.detectView's UIViews into the interactiveView.detectView but the outcome was the same.

It's rigged so the subviews change colour, but they change colour when they are not even touching, and when they do touch the wrong UIviews are changing colour

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

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

发布评论

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

评论(1

抱着落日 2024-09-02 20:52:59

我在使用convertRect时解决了我的问题。

我以为我会再次阅读该文档(相信我,我一直在阅读它),但我之前错过了一条关键信息。要使用 ConvertRect:toView: 使用该方法,视图需要成为文档中提到的转换操作的目标,但我使用视图本身作为目标而不是父视图

interactRect = [detectInteractView convertRect:[detectInteractView frame] toView:parentView];

这是错误的,我知道这篇文章中没有太多细节,但最终你不能使用相同的 UIView 作为目标视图,或者至少如果可以的话我无法让它在这里工作!

I worked out my issue when using convertRect.

I thought I'd read the documentation again (believe me I've been reading it) but I missed a key piece of information previously. To use convertRect:toView: to use the method the view needs to be target of the conversion operation as mentioned in the doc, but I was using the view itself as the target instead of the parent view

interactRect = [detectInteractView convertRect:[detectInteractView frame] toView:parentView];

This is wrong, I know there isn't many details in this post but ultimately you can't use the same UIView as the target view, or at least if you can I couldn't get it to work here!

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