简体   繁体   English

在CGAffineTransform之后获得UIView的中心

[英]Get center of a UIView after CGAffineTransform

Suppose I had a small UIView as a child/subview of a larger UIView, and that child could be moved around via some CGAffineTransforms. 假设我有一个小的UIView作为一个更大的UIView的子/子视图,那个孩子可以通过一些CGAffineTransforms移动。 How might the the parent know what the true 'center' of that view is within its own coordinate system? 父母如何知道该视图的真正“中心”在其自己的坐标系中? I have tried using the convertPoint routines with whatever is returned by child.center, but it isn't working... is 'center' completely bogus in this context or am I just using the wrong method? 我已经尝试将convertPoint例程与child.center返回的内容一起使用,但是它不起作用......在这种情况下,“中心”完全是假的还是我只是使用了错误的方法?

EDIT: 编辑:

After doing a bit of testing I noticed the following: 做了一些测试后,我注意到以下几点:

UIViews don't have an anchorPoint property, but they do have a center property. UIViews没有anchorPoint属性,但它们有一个center属性。 The center property is always calculated properly after applying transforms, except a translation transform, for which you have to do the following: 应用变换后,始终会正确计算center属性,但转换变换除外,您必须执行以下操作:

CGPoint realCenter = CGPointMake(myView.center.x + myView.frame.origin.x, ...);

As for CALayer s, they do have an anchorPoint property, but they lack a center property. 至于CALayer ,他们确实有一个anchorPoint属性,但是他们缺少一个中心属性。 So, what you want to do is calculate the center manually by doing calculations on the position property, anchorPoint property and the translation of your layer. 因此,您要做的是通过对position属性,anchorPoint属性和图层的转换进行计算来手动计算中心。

I can't provide any code, since I am not sure which method you are using, but to wrap it up, you have to roll out your own center calculator either ways. 我无法提供任何代码,因为我不确定您使用的是哪种方法,但要将其包装起来,您必须以任何方式推出自己的中心计算器。


Please look at the pictures below carefully (courtesy of Stanford iPhone Development course slides): 请仔细查看下面的图片(斯坦福iPhone开发课程幻灯片提供):

Before applying any rotation: 在应用任何轮换之前:

在此输入图像描述

After applying a 45° rotation: 施加45°旋转后:

在此输入图像描述

Conclusion: 结论:

Notice how the old center was (300, 225) and the new center is, well not new! 请注意旧中心是如何(300,225)和新中心,而不是新的! It's the same. 一样的。 If you are doing everything correctly, your center should be the same. 如果你正确地做了一切,你的中心应该是一样的。 If you have another point within the view that you'd like to calculate, then you'd have to that yourself. 如果您在视图中有另一个要计算的点,那么您自己就必须这样做。

Please also notice how the frame changed from (200, 100, 200, 250) to (140, 65, 320, 320). 另请注意框架如何从(200,100,200,250)更改为(140,65,320,320)。 This is just how UIKit does it's magic. 这就是UIKit如何发挥它的魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM