3

我有一个子视图(除了其他标准的 Cocoa Touch 控件)里面有 6 个按钮和标签。

在此处输入图像描述

我正在使用这个凌乱的代码在旋转事件上旋转和调整这些按钮和标签的大小。

- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        button1.frame = CGRectMake(20, 59, 130, 80);
        button2.frame = CGRectMake(170, 59, 130, 80);
        button3.frame = CGRectMake(20, 176, 130, 80);
        button4.frame = CGRectMake(170, 176, 130, 80);
        button5.frame = CGRectMake(20, 293, 130, 80);
        button6.frame = CGRectMake(170, 293, 130, 80);

        label1.frame  = CGRectMake(20, 147, 130, 21);
        label2.frame  = CGRectMake(170, 147, 130, 21);
        label3.frame  = CGRectMake(20, 264, 130, 21);
        label4.frame  = CGRectMake(170, 264, 130, 21);
        label5.frame  = CGRectMake(20, 381, 130, 21);
        label6.frame  = CGRectMake(170, 381, 130, 21);
    } else {
        button1.frame = CGRectMake(20, 59, 130, 60);
        button2.frame = CGRectMake(20, 155, 130, 60);
        button3.frame = CGRectMake(177, 59, 130, 60);
        button4.frame = CGRectMake(177, 155, 130, 60);
        button5.frame = CGRectMake(328, 59, 130, 60);
        button6.frame = CGRectMake(328, 155, 130, 60);

        label1.frame  = CGRectMake(20, 127, 130, 21);
        label2.frame  = CGRectMake(20, 223, 130, 21);
        label3.frame  = CGRectMake(177, 127, 130, 21);
        label4.frame  = CGRectMake(177, 223, 130, 21);
        label5.frame  = CGRectMake(328, 127, 130, 21);
        label6.frame  = CGRectMake(328, 223, 130, 21);
    }
}

这有点乱,但工作正常,我可以精确控制视图中的元素位置

在此处输入图像描述

顺便说一句,我想知道是否有两个不同的视图并在旋转时翻转它们在“cpu 功率”和内存消耗方面是否更有效(我认为有一个视图而不是两个视图对内存更好,但我可能是错的,我对ios编程很陌生)。

感谢您的任何建议!

4

2 回答 2

2

You ask:

By the way I'm wondering if having two different views and flipping them on rotation is more efficient regarding "cpu power" and memory consumption ...

I believe that the opposite is true. It's far more efficient to have a single view for which you simply invoke your method from viewWillLayoutSubviews (in iOS 5, in iOS 4 I generally use willAnimateRotationToInterfaceOrientation). The efficiency gain is modest, but if efficiency is your goal, I think a single view is the way to go. You might want to use the separate views if they're radically different and thus your code would become hard to manage with a single view, but otherwise I stick with one view. Personally, I also think it's a stronger user interface to have the controls animate into place as you rotate the device.

Personally, when I have content that reorganizes on orientation changes like this, I try to refrain from using hard coded coordinates, but rather use the dimensions of the view to algorithmically determine the the layout of my controls (e.g. how many per row) and from there, determine their coordinates. This way, it takes care of not only landscape versus portrait, but also makes Universal apps (especially if there's ever other devices with different screen sizes in the future) easier, too. I think we can also look forward to iOS 6 also offering some nice enhancements to laying out controls (though it will be a while before we will feel comfortable developing apps that require iOS 6).

于 2012-09-08T20:31:14.820 回答
2

现在我正在处理你写的类似代码。这个项目很大,很难遵循什么地方去修改。它是为 iOS 3 编写的,现在客户端需要进行一些修改。如果 iOS 不会更改 pushModal 并且会在 iOS6 中被弃用,那么问题就不会那么大了……在我的情况下,需要重写 GUI 框架,并且我有 Portait / Landscape 要求。如果您愿意,请查看几个链接:question1 question2 question3 question4

对于不可见的 iOS 更改,很难概述该代码,尤其是在 2 年后。我不会重复以前工作过的程序员错误。

在您的情况下,我会从不同的解决方案中进行选择(但这肯定不是):

解决方案 1:为横向和纵向创建不同的 xib。具有模块化的优点:以后如果由于各种原因不需要 Landscape,很容易从代码中取消链接。具有创建新视图的缺点(它可以被缓存)但仍然是不同的对象并且需要模型-View2-控制器之间的数据同步。

解决方案2:仅使用 1 个 xib 文件并通过在尺寸检查器(第 5 个选项卡)设置它们的属性来自动布局组件。非常难配置

解决方案 3:在该 xib 文件中创建组件横向并布局这些组件以匹配预期的设计、大小,并在运行时读取它们的大小并设置它,就像现在这样,但可以可视化编辑。

Storyboard 或 xib,对我来说几乎一样,可以有 2 个 UIViewController 并弹出/推送 Portait/Landscape 以不使用旋转填充堆栈:)

如果您根本不旋转,速度 ofc 是最好的,但如果您仍然想要,也许 1 xib 中的 2 个组件,因为不需要更改可见性 2-4 次,这将触发很多 od 函数调用: viewwillappear、wiewdidapear、viewwilldisapers 等等。

于 2012-09-08T19:29:52.910 回答