将 CGRect 重新缩放为特定的 CGSize

问题描述 投票:0回答:2

我有一个关于将框架缩放到特定尺寸的问题。

我有一个

CGRect
,想将其调整为特定的
CGSize

我想根据我的重新缩放值按比例移动此

CGRect
的中心。

uikit cgaffinetransform cgrect
2个回答
0
投票

如果您碰巧要修改

UIView
,您可以采用以下方法:

CGPoint previousCenter = view.center;
// Set width and height here:
view.frame = CGRectMake(view.frame.origin.x,view.frame.origin.y, width, height); 
view.center = previousCenter;

这将在更改视图大小的同时保持中心点。

我对你的问题有点困惑,所以我可能没有正确回答。如果您使用

CGAffineTransform
进行缩放,正如您的标签所示,那完全是另一回事。


0
投票

您可以使用

CGRectInset(rect, x, y)
。这会将 CGRect 插入
x
y
,并将原点推入
x
y
。 (https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html#//apple_ref/c/func/CGRectInset

CGSize targetSize = CGSizeMake(100.0f, 100.0f);
CGRect rect = CGRectMake(50.0f, 50.0f, 200.0f, 200.0f);
rect = CGRectInset(rect, roundf((rect.size.width - targetSize.width) / 2.0f), roundf((rect.size.height - targetSize.height) / 2.0f);

编辑:请注意,我使用的是两种尺寸之间的差异,减半。我的理由是

CGRectInset
会影响整个矩形。我的意思是...

CGRect rect = CGRectMake(0, 0, 10, 10);
rect = CGRectInset(rect, 2, 2);

rect is now a CGRect with (2, 2, 6, 6)
© www.soinside.com 2019 - 2024. All rights reserved.