如何在旋转的UIView中使用CGRectContainsPoint() [英] How to use CGRectContainsPoint() with rotated UIView

查看:232
本文介绍了如何在旋转的UIView中使用CGRectContainsPoint()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView ,用户可以点击 UIView 来选择或突出显示它所代表的app'thing'。我使用 CGRectContainsPoint(thing.frame,tapPoint)来实现这一点,其中 thing.frame 是<的框架code> UIView 和 tapPoint 是来自 UITapGestureRecognizer 的点击点。这很完美。

I have a UIView, and the user can tap the UIView to 'select' or highlight the in-app 'thing' that it represents. I use CGRectContainsPoint(thing.frame,tapPoint) to achieve this, where thing.frame is the frame of the UIView, and tapPoint is the tapped point from a UITapGestureRecognizer. This works perfect.

..除非 UIView 通过设置转换 property(带有 CGAffineTransform 值)。当像这样旋转 UIView 时,框架变成一个封闭旋转视图的扁平方块。

..except when UIView is rotated by setting the transform property (with a CGAffineTransform value). When the UIView is rotated like this, the frame becomes a flat square that encapsulates the rotated view.

以下是问题的说明(框架属性标记为A,视觉 UIView 边界标记为B):

Here is an illustration of the problem (frame property is labeled A, and the visual UIView bounds are labeled B):

未转动时

+------------------+
|      A == B      |
+------------------+

旋转时

+-----------------+
|  A        .     |
|         .   .   |
|       .       . |
|     .       .   |
|   .    B  .     |
| .       .       |
|   .   .         |
|     .           |
+-----------------+

我想捕获在rect B 范围内的水龙头( UIView 的真实界限,已旋转) ,但不是当它们只在rect A 属性值 UIView )而不是 B

I want to capture taps that are within the bounds of rect B (the true bounds of UIView, rotated), but NOT when they're only within rect A (the value of the frame property of UIView) and not B.

我如何计算给定的水龙头? point是否在旋转的 UIView 的真实边界/框架/边框内?这有方便的方法吗?或者我需要使用自己的几何计算 B 的坐标和尺寸?

How might I calculate whether a given tap point is within the true bounds/frame/borders of the rotated UIView? Is there a convenience method for this? Or would I need to calculate the coordinates and dimensions of B using my own geometry?

(如果是后者,请包含一个建议,以便我们尽可能完整地回答。谢谢!)

(If the latter, please include a suggestion so we can make the answer as complete as possible. Thanks!)

推荐答案

你发现了一个基本的当他们第一次为框架和边界工作时,每个人都有的绊脚石。

You're discovering the one fundamental stumbling block that everybody has when they first work for frame and bounds.

帧是视图适合的最小可能(非旋转)矩形,考虑到变换。意思是,如果你要测试触摸,你可以登录视图周围的可用空间,只要它在最小的可能矩形内。

Frame is the smallest possible (non-rotated) rectangle in which a view fits in, taking into account transformation. Meaning, if you were to test for touches, you could log in the available space around the view so long as it was within that smallest possible rectangle.

对于视觉效果,想象蓝色方块是转换后的 UIView 。视图周围的蓝色边框表示它的框架。注意,即使视图被转换,它的帧仍然是未转换的并处于标准位置。如果传递 frame 而不是 bounds

For the visual, imagine the blue square is a transformed UIView. The blue border around the view represents it's frame. Notice how, even though the view is transformed, that it's frame remains un-transformed and in standard position. The green area represents the areas that are touchable if frame is passed instead of bounds:

另一方面,边界代表接收器的矩形相对于自身,考虑到变换,因此通过传递边界来测试视图中的点(在 -convertPoint:toView: call)将正确返回给定的触摸(点)是否与视图相交。

Bounds, on the other hand, represents the reciever's rectangle with respect to itself, taking into account transformations, and so testing for points in the view by passing bounds (after a -convertPoint:toView: call) will correctly return whether or not a given touch (point) intersects the view.

这篇关于如何在旋转的UIView中使用CGRectContainsPoint()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆