【问题标题】:Landscape mode UIView.center gives Portrait mode coordinates横向模式 UIView.center 给出纵向模式坐标
【发布时间】:2011-11-18 10:20:32
【问题描述】:

我有一个应用程序,它以横向模式运行,我在手机上调试时将其保持在横向模式。我从主视图控制器上的 UIView 中获取中心值,我需要它来将项目放在屏幕的中心,因为它是通用的,所以我需要它是可变的,而不是设置为 iPhone 屏幕尺寸。

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

委托是我的应用程序中的一个视图控制器,中心功能是我想要移动到中心的对象之一。

- (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:x y:y];
}

- (void)_move: (NSTimeInterval)duration
            curve:(int)curve x:(CGFloat)x y:(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];

}

现在对我来说这没有多大意义。

【问题讨论】:

标签: iphone ios uiview


【解决方案1】:

使用边界,而不是框架。框架位于超级视图的坐标系中,对于您的根视图来说,它是窗口 - 这不会以不同的方向旋转。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多