The Origin of the Rectangle in CGRectMake

I have a simple screen in my iPhone app where I want a 320x100 rectangle at the bottom of the screen to capture a touch. Here is my code inside touchesBegan:withEvent:

for (UITouch *touch in touches) {

    CGPoint touchPoint = [touch locationInView:self.view];

    NSLog(@"touch @ %f, %f", touchPoint.x, touchPoint.y);

    // build a rectangle where we want to capture a URL tap
    CGRect rectangle = CGRectMake(0, 480, 320, 100);

    NSLog(@"midX, midY = %f, %f", CGRectGetMidX(rectangle), CGRectGetMidY(rectangle));

    // check to see if they tapped the URL    
    if (CGRectContainsPoint(rectangle, touchPoint)) {
        NSLog(@"You touched inside the rectangle.");
    }        
}

Now this code is not working properly ... a log from the midpoint of the rectangle shows that my rectangle is built in midX, midY = 160.000000, 530.000000. According to the CGPoint documentation, the origin (0, 480)is the bottom left corner, but it acts like the origin is the top left corner.

When I change the beginning of my rectangle to 0, 380, everything works as intended. Maybe this morning I did not work with caffeine, but why do I see this discrepancy between documentation and execution?

+3
2

, .

UIKit (0, 0) , y .

CoreGraphics (0, 0) , y . CG UIKit , , CG -drawRect:, .

API UIKit, .

+11

. , . (AFAIK, Mac, , , .)

iPhone, , ,

+1

Source: https://habr.com/ru/post/1730864/


All Articles