如何将CGRect的大小增加一定的百分比值? [英] How can I increase the size of a CGRect by a certain percent value?

查看:352
本文介绍了如何将CGRect的大小增加一定的百分比值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将CGRect的大小增加一定的百分比值?我应该使用某种形式的 CGRectInset 来做这件事吗?

How can I increase the size of a CGRect by a certain percent value? Should I use some form of CGRectInset to do it?

示例:

假设我有一个CGRect: {10,10,110,110}

Assume I have a CGRect: {10, 10, 110, 110}

I想要将它的大小(保持相同的中心点)增加20%到:

I want to increase it's size (retaining the same center point) by 20% to:

{0,0,120,120}

推荐答案

如果您愿意,可以使用 CGRectInset

You can use CGRectInset if you like:

double pct = 0.2;
CGRect newRect = CGRectInset(oldRect, -CGRectGetWidth(oldRect)*pct/2, -CGRectGetHeight(oldRect)*pct/2);

要减小尺寸,请删除 - s。

To decrease the size, remove the -s.

旁注 CGRect 比<$大20% c $ c> {10,10,100,100} 是 {0,0,120,120}

编辑:如果打算按面积增加,那么这样做(即使对于那些矩形也是如此) 't square):

Edit: If the intention is to increase by area, then this'll do it (even for rectangles that aren't square):

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);

这篇关于如何将CGRect的大小增加一定的百分比值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆