Determining if a point in a view is within a subviews bounds
Asked Answered
S

1

8

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

Sternutatory answered 2/12, 2012 at 2:26 Comment(2)
How can a point be orthogonal to a rectangle?Sweeney
No. The rectangle is orthogonal to the subviews bounds. If it's easier, I can deal with an answer to whether the point is within the subviews bounds (not the subview's frame).Sternutatory
S
21

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!");
}
Sweeney answered 2/12, 2012 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.