Search code examples
iosobjective-ccoordinate-systems

Pixel position of UIImageView in coordinate system


I am trapped in an interesting situation where I have values 1.4, 1.4 for x,y of rect so which pixel dot will it start putting the imageview from.. and why.. any documentation link ?

Any light on it please.

Thanks.


Solution

  • This is not something taken from any doc, but this is what happens with a quick code, ran on iOS 7 with an iPhone with retina :

    view = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 20, 20)];
    view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view];
    
    view2 = [[UIView alloc] initWithFrame:CGRectMake(10.4, 70, 20, 20)];
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    view3 = [[UIView alloc] initWithFrame:CGRectMake(11, 90, 20, 20)];
    view3.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view3];
    

    The first view is the reference. I know for sure from the code that its x origin is at 20px.

    Be careful here, 10 points is x=20px, and 11 points is x=22px, because of the retina @2x resolution. You speak in points in code, and have a pixels result on screen.

    I tried changing progressively the x coordinate. Here is what I found :

    • 9.75 < x <= 10.25 : the red square starts at x = 20px (green and red are aligned left).

    • 10.25 < x <= 10.75 : the red square starts at x = 21px (red is not aligned with green or blue).

    • 10.75 < x <= 11.25 : the red square starts at x = 22px (green and blue are aligned left).

    This is based on my own observation from a quick simulation. I haven't searched for any official reference though.

    Here is an image with 3 positions : 10.1, 10.4 and 10.8

    Comparaison