iOS如何找到CGRect的更大尺寸?

时间:2015-07-29 17:37:15

标签: ios geometry core-graphics cgrect

我知道核心图形有许多辅助函数,例如CGRectGetMidX。我正在使用autolayout并使用像

这样的代码
    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)]

有没有办法用替换{{1>}函数来获取矩形的最大尺寸,例如getMaxDimension(self.view.bounds)?

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

1 个答案:

答案 0 :(得分:4)

我认为

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

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