如何为我的iOS应用程序构建启动器?

时间:2012-09-24 22:21:30

标签: ios ios5 launcher

我正在尝试为我的应用构建一个主页启动器... 像Three20那样的东西。

enter image description here

我不想重新发明轮子,但像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);
    }
}

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

2 个答案:

答案 0 :(得分:2)

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

https://github.com/fieldforceapp/openspringboard

答案 1 :(得分:1)

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

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

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

}