尺寸改变后计算插入的CGRect的新原点 [英] Calculating new origin of insetted CGRect after its size changes

查看:106
本文介绍了尺寸改变后计算插入的CGRect的新原点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CGRect ACGRect B,其中BA的内部居中(CGRect包含x和y的原点以及矩形的高度和宽度尺寸).如果我按一定比例增加A的宽度和高度,并且也以相同的比例增加B的宽度和高度,则将B的x原点和B的y原点相乘以相同的比例(分别代表宽度和高度),随着两个比例都按新比例增长时,B会保持在A的中心吗?我已经在几种不同的情况下对此进行了测试,并且它可以工作,但是我只是想验证一下它是否可以在所有情况下都可以工作,因为我的数学并不那么聪明.

此外,我想知道是否有一种方法可以简单地让您将CGRect的所有值乘以该比例,而不必手动执行(在文档中找不到该值).

更新:实际上,这是行不通的...试图考虑一种方法,该方法可以使我在两个视图的大小成比例增加后在另一个视图中正确放置一个视图.

解决方案

是的,您的建议有效,但仅当外部CGRect的原点为0,0或将其原点乘以该因子时.如果您不这样做,则内部矩形将移至右下角.

如果您同时乘以原点和大小,将会发生以下情况:

如果不乘以外部矩形的原点,则会发生这种情况:

从您的问题来看,您尚不清楚要实现的目标. 如果要放大CGRect并将其重新居中,请使用以下功能:

//  center a CGRect in another one
static inline CGRect ALRectCenterInRect(CGRect outerRect, CGRect innerRect)
{
    return CGRectMake(CGRectGetMidX(outerRect)-innerRect.size.width/2, CGRectGetMidY(outerRect)-innerRect.size.height/2, innerRect.size.width, innerRect.size.height);
}

//  multiply each value of a CGRect with factor
//      combine with CGRectIntegral() to prevent fractions (and the resulting aliasing)
static inline CGRect ALRectMultiply(CGRect rect, CGFloat factor)
{
    return CGRectMake(rect.origin.x*factor, rect.origin.y*factor, rect.size.width*factor, rect.size.height*factor);
}

如何使用它们:

CGRect centeredInnerRect = ALRectCenterInRect(outerRect, innerRect);

CGRect multipliedRect = ALRectMultiply(someRect, 1.5);

但是,当处理CGRect s时,通常大约是UIView s.如果要在UIView的超级视图中居中,请执行以下操作:

someSubview.center = CGPointMake(CGRectGetMidX(someSuperview.bounds), CGRectGetMidY(someSuperview.bounds));

如果内部视图与外部视图具有相同的超级视图,则只需执行以下操作即可将其在外部视图中居中:

innerView.center = outerView.center;

I have a CGRect A and CGRect B where B is centered inside of A (a CGRect contains the x and y origin and height and width size of a rectangle). If I increase the width and height of A by some proportion, and also increase the width and height of B by that same proportion, will multiplying the x origin of B and the y origin of B by this same proportion (for the width and height respectfully), will that keep B in the center of A as both grow by the new proportion? I've tested this out in a few different scenarios and it works, but just wanted to verify it'll work for all situations as I am not that sharp in math.

Also, was wondering if there is a method that will simply allow you to multiply all values of a CGRect by this proportion without having to do it manually (couldn't find one in the docs).

UPDATE: Actually, this will not work...trying to think of a methodology that will allow me to correctly position a view within another view after a proportional increase in size for both.

解决方案

Yes, what you proposed works, but only if the origin of the outer CGRect is 0,0 or if you multiply its origin by the factor, too. If you don't do that, the inner rect will be shifted to the bottom right.

Here's what happens if you multiple both origin and size:

If you don't multiply the outer rect's origin, this happens:

From your question, it isn't entirely clear what you're trying to achieve. If you want to enlarge a CGRect and (re)center it another one, use these functions:

//  center a CGRect in another one
static inline CGRect ALRectCenterInRect(CGRect outerRect, CGRect innerRect)
{
    return CGRectMake(CGRectGetMidX(outerRect)-innerRect.size.width/2, CGRectGetMidY(outerRect)-innerRect.size.height/2, innerRect.size.width, innerRect.size.height);
}

//  multiply each value of a CGRect with factor
//      combine with CGRectIntegral() to prevent fractions (and the resulting aliasing)
static inline CGRect ALRectMultiply(CGRect rect, CGFloat factor)
{
    return CGRectMake(rect.origin.x*factor, rect.origin.y*factor, rect.size.width*factor, rect.size.height*factor);
}

How to use them:

CGRect centeredInnerRect = ALRectCenterInRect(outerRect, innerRect);

CGRect multipliedRect = ALRectMultiply(someRect, 1.5);

However, when dealing with CGRects, it's usually about UIViews. If you want to center a UIView in its superview, do this:

someSubview.center = CGPointMake(CGRectGetMidX(someSuperview.bounds), CGRectGetMidY(someSuperview.bounds));

If the inner view has the same superview as the outer view, you can simply do this to center it in the outer view:

innerView.center = outerView.center;

这篇关于尺寸改变后计算插入的CGRect的新原点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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