OGeek|极客世界-中国程序员成长平台

标题: iOS如何找到CGRect的较大尺寸? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:19
标题: iOS如何找到CGRect的较大尺寸?

我知道核心图形有很多辅助函数,例如CGRectGetMidX。我正在使用自动布局并使用像

这样的代码
    float height = self.view.bounds.size.height * 0.08;
    UIImageView* key = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,height,height)];

我可以使用三元运算符,但它看起来真的很难看,不可读:

float dimension = (self.view.bounds.size.height> self.view.bounds.size.width ?  self.view.bounds.size.height: self.view.bounds.size.width) * 0.08;
UIImageView* key = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,dimension,dimension)]

有没有办法将 self.view.bounds.size.height 替换为 一个获取矩形最大尺寸的函数,例如 getMaxDimension(self.view.bounds) ?

我知道我可以用 if 语句编写自己的语句,但是我很感兴趣,如果 iOS 中有一些东西可以让我使用单个内置函数来执行此操作,这样我就可以在多个项目中使用相同的方法。



Best Answer-推荐答案


我认为

MAX(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));

可能是您从内置函数中获得的最易读的代码。 几何函数: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/ MAX 是来自 Objective-C 运行时的宏。 如果您希望代码比这更具可读性,只需编写您自己的函数即可。

关于iOS如何找到CGRect的较大尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707394/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4