横向模式UIView.center给出纵向模式坐标 [英] Landscape mode UIView.center gives Portrait mode coordinates

查看:56
本文介绍了横向模式UIView.center给出纵向模式坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app, it runs in landscape mode, I hold it in landscape mode when debugging on my phone. I pick up the center value from the UIView on my main view controller, I need this to put items in the center of the screen, and since it is universal I need this to be variable and not set to iPhone screen sizes.

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

When I do this and read the x and y from the CGPoint returned by view.center I get x = 170 y = 240

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

The delegate is the one viewcontroller in my app, the center function is one of the object I want to move to the center.

- (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];

}

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

Now to me this does not make much sense.

推荐答案

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

Use bounds, not frame. Frame is in the superview's coordinate system which for your root view is the window - this does not rotate in different orientations.

边界是视图自身的坐标系,因此在子视图上设置中心时要使用适当的矩形,因为中心也以超级视图的坐标系表示.

Bounds is in the views own coordinate system so is the appropriate rect to use when setting center on subviews, since center is also expressed in the superview's coordinate system.

这篇关于横向模式UIView.center给出纵向模式坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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