Determining if a point in a view is within a subvi

2019-04-07 00:28发布

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)?

1条回答
Viruses.
2楼-- · 2019-04-07 00:59

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!");
}
查看更多
登录 后发表回答