gpt4 book ai didi

objective-c - CGRectMake 中矩形的起源

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:23 24 4
gpt4 key购买 nike

我的 iPhone 应用程序中有一个简单的屏幕,我希望在屏幕底部有一个 320x100 的矩形来捕捉触摸。这是我在 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.");
}
}

现在此代码无法按预期工作...矩形中点的日志显示我的矩形构建在 midX, midY = 160.000000, 530.000000 处。根据 CGPoint 文档,原点 (0, 480) 是左下角,但这就像原点是左上角一样。

当我将矩形的原点更改为 0, 380 时,一切都按预期进行。也许我今天早上还没有适当摄入咖啡因,但为什么我会看到文档和执行之间的这种差异?

最佳答案

原点是在左上角还是左下角实际上取决于坐标系。

在 UIKit 中,(0, 0) 位于左上角,y 轴向下增长。

在 CoreGraphics 中,(0, 0) 位于左下角,y 轴向上增长。为了让 CG 适应 UIKit,默认情况下会应用垂直反射,这就是为什么如果您直接使用 -drawRect: 中的 CG 函数绘制图像或字符串,您会将它们颠倒过来。

在您的例子中,您从 UIKit API 获取点和矩形,因此原点位于左上角。

关于objective-c - CGRectMake 中矩形的起源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2177207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com