作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如何将 CGRect 的大小增加一定的百分比值?我应该使用某种形式的 CGRectInset
来做到这一点吗?
例子:
假设我有一个 CGRect:{10, 10, 110, 110}
我想将其大小(保留相同的中心点)增加 20% 以:
{0, 0, 120, 120}
最佳答案
如果你愿意,你可以使用CGRectInset
:
double pct = 0.2;
CGRect newRect = CGRectInset(oldRect, -CGRectGetWidth(oldRect)*pct/2, -CGRectGetHeight(oldRect)*pct/2);
要减小大小,请删除 -
。
旁注:CGRect
比 {10, 10, 100, 100}
大 20% 是 {0 , 0, 120, 120}
.
编辑:如果打算按面积增加,那么这样做就可以了(即使对于非正方形的矩形也是如此):
CGFloat width = CGRectGetWidth(oldRect);
CGFloat height = CGRectGetHeight(oldRect);
double pct = 1.2; // 20% increase
double newWidth = sqrt(width * width * pct);
double newHeight = sqrt(height * height * pct);
CGRect newRect = CGRectInset(oldRect, (width-newWidth)/2, (height-newHeight)/2);
关于ios - 如何将 CGRect 的大小增加一定的百分比值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918886/
我是一名优秀的程序员,十分优秀!