菜鸟教程小白 发表于 2022-12-13 11:19:59

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


                                            <p><p>我知道核心图形有很多辅助函数,例如<code>CGRectGetMidX</code>。我正在使用自动布局并使用像 </p> 这样的代码

<pre><code>    float height = self.view.bounds.size.height * 0.08;
    UIImageView* key = [ initWithFrame:CGRectMake(0,0,height,height)];
</code></pre>

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

<pre><code>float dimension = (self.view.bounds.size.height&gt; self.view.bounds.size.width ?self.view.bounds.size.height: self.view.bounds.size.width) * 0.08;
UIImageView* key = [ initWithFrame:CGRectMake(0,0,dimension,dimension)]
</code></pre>

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

<p>我知道我可以用 if 语句编写自己的语句,但是我很感兴趣,如果 iOS 中有一些东西可以让我使用单个内置函数来执行此操作,这样我就可以在多个项目中使用相同的方法。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为</p>

<pre><code>MAX(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
</code></pre>

<p> 可能是您从内置函数中获得的最易读的代码。
几何函数:
<a href="https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/</a>
MAX 是来自 Objective-C 运行时的宏。
如果您希望代码比这更具可读性,只需编写您自己的函数即可。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS如何找到CGRect的较大尺寸?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31707394/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31707394/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS如何找到CGRect的较大尺寸?