objective-c - 更改 iOS 中的坐标方向

标签 objective-c ios xcode

我的 GLKViewController 子类中有一些如下所示的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    NSLog(@"touch has begun!: %f %f",point.x,point.y);
    float xw = [self.view bounds].size.width;
    float yw = [self.view bounds].size.height;
    NSLog(@"touch -- %f %f", point.x / xw, point.y / yw);
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationLandscapeRight == toInterfaceOrientation;
}

此外,我在“iPhone/iPod 部署信息”中将唯一支持的设备方向设置为“横向右”。

通过运行上述代码进行测试,左上角似乎是 (0,0)。然而,我有另一个与此类似的测试项目,其中 (0,0) 点位于左下角。

到底是什么决定了坐标系的方向?如果我希望我的 (0,0) 点位于左下角而不是左上角,我该怎么做?

最佳答案

在 iOS 上,(0,0) 处的角(称为原点)应始终位于左上角。我不知道有一种方便的方法来改变它,除了子类化 UIView 并处理所有各种算术 - 计算你的边界/框架,布置你的 subview ,并绘制你的内容 - 你自己。

在 Mac 上,原点略有不同:默认位于左下角。但是,您可以子类化 NSView 并从 -isFlipped 方法返回 YES 来向 View 层次结构表明您希望坐标的行为与 iOS 类似,原点位于上层向左。

我不知道有什么简单的方法可以让您意外地在 iOS 的左下角看到原点。如果您发布其他测试项目中的一些代码,我可以扩展此答案来解决它。

关于objective-c - 更改 iOS 中的坐标方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501837/

相关文章:

ios - 根据区域设置将所有字符串大写 - Swift

ios - 在哪里可以找到 Fabric 和 Crashlytics for firebase 的框架?

ios - Google Cast iOS SDK v3.3 存档上传问题

iphone - objective-c 中的后台任务

ios - 在 UITableView 上激活 Speech to Text

ios - 在变量 iOS 中存储完成处理程序( objective-c )

iphone - Xcode 4 中的 "Read/Write class files"在哪里?

iphone - Objective-C:类级别函数的含义?

ios - Xcode 如何在 iOS SDK 中配置 Architectures

objective-c - XCode编译IOS App报错 "file not found: -fobjc-arc"