Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert screen coordinate to UIView local point

I've seen many examples of how you can convert local point of an UIView to screen coordinate. For example,

CGPoint subContentLocalPoint = subContent.frame.origin ;
CGPoint basePos = [subContent convertPoint:subContentLocalPoint toView:nil] ;

But how does one do the opposite? (i.e. convert screen coordinates to a UIView local point.) For example,

Screen Coordinate - x:90 y:100 UIView content - x:0 y:0 // because this is where the UIView object is on the screen.

like image 789
Delamore Avatar asked Oct 01 '14 03:10

Delamore


2 Answers

Screen coordinates to specific view:

UIView* specificView = ...;
CGPoint pointInScreen = ...;
CGPoint pointInWindow = [specificView.window convertPoint:pointInScreen fromWindow:nil];
CGPoint pointInView = [specificView convertPoint:pointInWindow fromView:nil];
like image 183
fluidsonic Avatar answered Sep 22 '22 01:09

fluidsonic


Try this if you want to get screen position from position of subContent.

[subContent convertPoint:subContentLocalPoint fromView:[UIApplication sharedApplication].keyWindow];

like image 27
darkdongdong Avatar answered Sep 22 '22 01:09

darkdongdong