How do we get the "visual" coordinates of an object? For example, getting the frame of a textfield within a UIScrollView will give me the X and Y relative to the UIScrollView, but I want to know the X and Y as if they were shown in the view.
Is there a built in function?
Once you have the point as a CGPoint you can call:
// Objective-C
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
// C#'s UIView class contains this method:
PointF ConvertPointToView (PointF point, UIView target);
on you UIView.
pass in you point in your views co-ordinates as point. And for view pass in
// Objective-C
[UIWindow keyWindow]
// C#
UIWindow.KeyWindow
This will then return a CGPoint (PointF in C#) converted into the windows coordinate system!
So for example:
// Objective-C
CGPoint convertedPoint = [myScrollView convertPoint:textField.frame.origin
toView:[UIWindow keyWindow]];
// C#
var convertedPoint = myScrollView.ConvertPointToView (
textField.Frame.Location, UIWindow.KeyWindow);
Hope this helps :)
UIView has some convertPoint and convertRect functions:
- convertPoint:toView:
– convertRect:toView:
etc.
Might just be me, but wouldnt this work fine?
- (CGRect) convertView:(UIView*)view
{
CGRect rect = view.frame;
while(view.superview)
{
view = view.superview;
rect.origin.x += view.frame.origin.x;
rect.origin.y += view.frame.origin.y;
}
return rect;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With