Delamore
Delamore

Reputation: 53

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.

Upvotes: 5

Views: 3543

Answers (2)

fluidsonic
fluidsonic

Reputation: 4676

Screen coordinates to specific view:

UIView* specificView = ...;
CGPoint pointInScreen = ...;
CGPoint pointInWindow = [specificView.window convertPoint:pointInScreen fromWindow:nil];
CGPoint pointInView = [specificView convertPoint:pointInWindow fromView:nil];

Upvotes: 6

darkdongdong
darkdongdong

Reputation: 65

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

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

Upvotes: 1

Related Questions