风景模式UIView.center给出了Potrait模式坐标

好的,这是我的困境,我觉得这真的很奇怪,而且很无能。 我有一个应用程序,它在横向模式下运行,我在手机上debugging时保持横向模式。 我从我的主视图控制器UIView中取得中心值,我需要这个项目放在屏幕的中心,因为它是通用的,我需要这是可变的,而不是设置为iPhone的屏幕尺寸。

当我这样做,并从view.center返回的CGPoint读取x和y,我得到x = 170 y = 240

代表是我的应用程序中的一个viewcontroller,中心function是我想要移动到中心的对象之一。

- (void) center { CGPoint center = ((UIViewController*)delegate).view.center; CGSize size = self.frame.size; double x = center.x - size.width/2 - location.x; double y = center.y - size.height/2 - location.y; [self _move:1.0 curve:UIViewAnimationCurveEaseInOut x:xy:y]; } - (void)_move: (NSTimeInterval)duration curve:(int)curve x:(CGFloat)xy:(CGFloat)y { // Setup the animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:duration]; [UIView setAnimationCurve:curve]; [UIView setAnimationBeginsFromCurrentState:YES]; // The transform matrix CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); self.transform = transform; // Commit the changes [UIView commitAnimations]; } 

现在对我来说这没有什么意义。 任何人有任何想法如何解决这个问题?

使用边界,而不是框架。 框架在超视图的坐标系统中,为你的根视图是窗口 – 这不会以不同的方向旋转。

界限在视图自己的坐标系中,因此在设置子视图的中心时使用适当的矩形,因为中心也在超视图的坐标系中表示。