确定视图中的某个点是否在子视图范围内 [英] Determining if a point in a view is within a subviews bounds

查看:178
本文介绍了确定视图中的某个点是否在子视图范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个UIView parentView和一个子视图childView,它以相对于parentView的某个未知角度旋转。确定parentView中的一个点(我知道parentView的坐标系中的坐标)是否在childView的坐标系中的矩形内是最有效的方法(矩形与其边界正交,但不等于它的边界,可能与parentView的正交不正确)边界)?

Suppose I have a UIView parentView and a subview childView that is rotated at some unknown angle relative to parentView. What is the most efficient way to determine if a point within parentView (I know the coordinates in parentView's coordinate system) is within a rectangle in childView's coordinate system (the rectangle is orthogonal to, but not equal to its bounds and probably not orthogonal to parentView's bounds)?

推荐答案

将点转换为子视图的坐标系,然后使用 CGRectContainsPoint

Convert the point to the subview's coordinate system and then use CGRectContainsPoint:

CGPoint pointInSubview = [subview convertPoint:pointInSuperview fromView:superview];
if (CGRectContainsPoint(rectInSubview, pointInSubview)) {
    NSLog(@"We have a winner!");
}

这篇关于确定视图中的某个点是否在子视图范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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