Determining if a point in a view is within a subvi

2019-04-07 00:52发布

问题:

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:

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!");
}