【问题标题】:iOS how to find the larger dimension of a CGRect?iOS如何找到CGRect的较大尺寸?
【发布时间】:2015-07-29 17:37:15
【问题描述】:

我知道核心图形有很多辅助函数,比如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 中有什么东西可以让我用一个内置函数来做这件事,这样我就可以在多个项目中使用相同的方法。

【问题讨论】:

    标签: ios geometry core-graphics cgrect


    【解决方案1】:

    我认为

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2020-10-24
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多