24

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?

hpique
  • 119,096
  • 131
  • 338
  • 476

1 Answers1

54

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];
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Wish I'd known that a couple of weeks ago :) – jowie Dec 04 '10 at 16:12
  • 1
    +1 -- this totally saved my bacon, after I'd been messing with my own, what turned out to be, reimplementation of exactly that. Nice! :) – Olie Jun 28 '12 at 21:00