0

我正在尝试为我的应用程序构建一个主页启动器...类似于Three20中的那个。

在此处输入图像描述

我不想重新发明轮子,但是像 Three20 这样的解决方案不是我要寻找的:它们没有更新到最新的 iOS 系统或设备(视网膜、ARC、iOS5/6),而且它们做的远比什么我需要(我基本上需要一组带有在设备旋转时旋转的标签的按钮)。

现在我正在尝试构建一个在设备旋转时旋转的 2x3 按钮网格,但代码看起来很糟糕,我需要添加 iPhone5 支持......

- (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(177, 59, 130, 60);
        button3.frame = CGRectMake(328, 59, 130, 60);
        button4.frame = CGRectMake(20, 155, 130, 60);
        button5.frame = CGRectMake(177, 155, 130, 60);
        button6.frame = CGRectMake(328, 155, 130, 60);

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

“放置东西,旋转它”方法是构建这种组件的唯一方法吗?有没有其他选择?

4

2 回答 2

2

您可能想自己编写代码,但这里有一个很棒的开源启动器:

https://github.com/fieldforceapp/openspringboard

于 2012-09-30T20:50:15.507 回答
1

将按钮和标签放在视图中。然后,在旋转时,您必须重新排列视图。

此外,您可以使用 afor重新排列它们,这样您就可以通过一些代码来获得这种效果。

-(void)setScreenIconsToInterfaceRotation:(UIInterfaceOrientation)toInterfaceOrientation {
    NSArray * viewsArray = [NSArray arrayWithObjects:view1,view2,view3,view4,view5,view6, nil];  

    int currentIndex = 0;

    for (UIView * currentView in ViewsArray) {

        int x;
        int y;
        int xseparation = 20;
        int yseparation;
        int height;
        int width = 130;
        if (toInterfaceOrientation==UIDeviceOrientationPortrait || toInterfaceOrientation==UIDeviceOrientationPortraitUpsideDown)
        { 

            x = currentIndex %2;
            y = currentIndex /2;
            height = 101;
            if (currentIndex < 2) {
                yseparation = 59;
            } else {
                yseparation = 16;
            }

        } else {
            x = currentIndex %3;
            y = currentIndex /3;
            height = 81;
            if (currentIndex < 3) {
                yseparation = 59;
            } else {
                yseparation = 15;
            }

        }

        [currentView setFrame:CGRectMake(x*width+((x+1)*xseparation), y*height+((y+1)*yseparation), width, height)];


        currentIndex++;
        }

}

这是您的示例的代码

那么对于ios6

-(void)viewWillLayoutSubviews {
        [self setScreenIconsToInterfaceRotation:[[UIApplication sharedApplication] statusBarOrientation]];
    }

和ios5

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {

    [self setScreenIconsToInterfaceRotation:toInterfaceOrientation]; 

}
于 2012-09-30T20:54:05.013 回答