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)?
Asked
Active
Viewed 4,192 times
8
-
1How can a point be orthogonal to a rectangle? – rob mayoff Dec 02 '12 at 02:31
-
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). – Victor Engel Dec 02 '12 at 02:45
1 Answers
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!");
}

rob mayoff
- 375,296
- 67
- 796
- 848