Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGPoint relative to view

Tags:

Consider a screen point (CGPoint) and a view (UIView), which is somewhere inside the view hierarchy (it can be a subview of other views).

How can you convert the point to a point relative to the view's coordinates?

like image 705
hpique Avatar asked Dec 04 '10 10:12

hpique


1 Answers

First, convert the point from screen coordinates to the coordinates of your main window:

UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow]; CGPoint pointInWindowCoords = [mainWindow convertPoint:pointInScreenCoords fromWindow:nil]; 

Second, convert the point from window coords to view coords:

CGPoint pointInViewCoords = [myView convertPoint:pointInWindowCoords fromView:mainWindow]; 
like image 107
Ole Begemann Avatar answered Nov 09 '22 12:11

Ole Begemann