简体   繁体   中英

Confused by the Origin of an UIView

I always had the impression the origin of a View defines its position inside a parent view. But in my application the Origin is 0,0 no matter where I move my view on the Interface.

This is the code I use:

- (void)drawRect:(CGRect)rect
{
    float orginX = rect.origin.x;
    float orginY = rect.origin.y;

    NSLog(@"X-Origin %f.", orginX);
    NSLog(@"Y-Origin %f.", orginY);

Where is my mistake?

传递给drawRect:的rect是视图的边界,而不是框架,因此其原点将为0,0。

I believe the drawRect method is passed the frame relative to the view's origin, ie the origin will always be 0,0. This is because you should draw your view without knowing or caring where it is in relation to its superview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM