1

我很难弄清楚如何为我的 iPad 应用程序的一个屏幕处理不同的设备方向。这是我的情况:

~ 除一个标签外,所有屏幕都使用弹簧和支柱完美旋转。这个标签的问题是我希望它以非正统的方式(对角线)移动,因此弹簧和支柱(或调整面具的大小将不起作用)。

〜我正在考虑这样做的方式是这样的:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(newInterfaceOrientation))
    {
        self.myScreenLabel.frame = CGRectMake(600,0,400,100);
    }
    else {
        self.myScreenLabel.frame = CGRectMake(...//something); }
}

我也会用类似的逻辑检查 viewDidLoad。 If in portrait mode, put label at... else put label at....

我认为这会奏效;但是,我有点想知道是否有更好的设计方法来做到这一点。上面的方法,到处都有硬编码的数字;因此,有没有更好的方法来做到这一点?此外,这种方法没有利用这样一个事实,即我的标签在纵向模式的情节提要中完美定位,而我需要更改的只是横向。

对更好的设计有什么建议吗?

4

2 回答 2

3

第一个问题,我已经用你的方法实现了方向改变的行为,没有坏结果,如果你可以在旋转发生之前触发该方法。或者,您可以使用 NSNotifications 添加方向更改触发器:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];

然后添加一个方法,如:

 - (void) didRotate:(NSNotification *)notification {   
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationLandscapeLeft) {
        //your code here
    }
 }

关于你的第二个问题,像这样定位框架很可怕,但我想你意识到了这一点。相反,定位与其他事物相关的元素,就像一个支柱(也就是说,总是相对于它被支柱之间的距离定位小部件)。因此,使用窗口的框架、视图的框架或视图中的一些其他 UIView 子类来定位对象,而不是绝对数字。

于 2012-06-11T23:26:29.060 回答
0

A couple of thoughts:

  1. If iOS 5+, you might want to use viewWillLayoutSubviews, probably even more important given the comments in the iOS 6 release notes re modal views and the screen reorientation methods. This also has the advantage that your code is in one spot. Since I still support pre 5 (though I won't for much longer), I actually have dynamic checking of iOS version and invoke my viewWillLayoutSubviews from the other methods if pre 5.0, otherwise I let viewWillLayoutSubviews just do the heavy lifting.

  2. If your landscape orientation is radically different, you probably want to pursue Creating an Alternate Landscape Interface. I've never done this, but it seems like it's up your alley. There are also postings on SO about using different NIBs for different orientations. Not sure this makes sense in a storyboard environment, though.

  3. For these controls that we occasionally have to move around or resize based upon screen dimensions, I think most of us do it with viewWillLayoutSubviews. That's the entire purpose of that method (though I generally use it for labels whose height changes based upon the data contents and the screen width). I had never stopped to think that there might be another way. If you only have one control that you're moving around as you change your orientation, maybe you could create two additional, hidden controls, one for where you want your visible control in portrait, the other hidden control for where you want it in landscape (and in IB, you can toggle your view orientation to facilitate the layout of controls). Then in your viewWillLayoutSubviews set your frame of your actual visible control to be the frame of one of those two hidden controls (depending upon orientation, of course). That gets you out of the business of hardcoding frame coordinates in your code, and take advantage of the benefits of IB. This whole suggestion might be too cute by half, but it's an alternative approach if you don't want to go through the effort of my second point.

于 2012-06-11T23:43:19.077 回答