在旋转时重新定位UIButton矩阵

时间:2013-06-27 10:26:18

标签: objective-c button matrix rotation

我有一个X数量按钮的矩阵添加到视图控制器。在纵向模式下,它以完美的顺序显示按钮。但是,随着视图控制器的显示,我旋转到横向模式,按钮不会重新定位。但是,如果我旋转手机并且 然后 会显示视图控制器,它会重新定位横向模式按钮。

我已经坚持这个问题已经有一段时间了,并且不知道为什么当视图控制器已经出现时按钮不会旋转。我希望有人可以帮助我。

菲利普

以下是首先创建按钮的代码:

- (void)createButtonMatrix {   
     for (int rows = 0; rows < 11; rows++) {
                    for (int columns = 0; columns < 6; columns++) {
                        NSUInteger chapter =  columns +  rows * 6 + 1;
                        if (chapter > 50)
                            break;
                        chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                        chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                        chapterButtons.tag = chapter;
                        [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                        [createButtonArray addObject:chapterButtons];
                        [viewController2.view addSubview:chapterButtons];
                    }
                }
    }

现在在旋转时重新定位它们:

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

        if (orientation == UIInterfaceOrientationPortrait) {
              NSLog(@"Will rotate to portrait.");
            for (int rows = 0; rows < 11; rows++) {
                for (int columns = 0; columns < 6; columns++) {
                    NSUInteger chapter =  columns +  rows * 6 + 1;
                    if (chapter > 50)
                        break;
                    chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                    chapterButtons.tag = chapter;
                    [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                }
            }
        }

        else if (orientation == UIInterfaceOrientationLandscapeRight ||orientation == UIInterfaceOrientationLandscapeLeft) {
                NSLog(@"Will rotate to landscape.");
                for (int rows = 0; rows < 7; rows++) {
                    for (int columns = 0; columns < 9; columns++) {
                        NSUInteger chapter =  columns +  rows * 9 + 1;
                        if (chapter > 50)
                            break;
                        chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                        chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                        chapterButtons.tag = chapter;
                        [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                    }
                }

        }
    }

3 个答案:

答案 0 :(得分:1)

如果我很了解您的代码,您将在方向更改时重新创建按钮,但这些按钮永远不会添加到视图中。这是错的。 您应该只更改createButtonArray(填入createButtonMatrix())每个按钮的位置:不要在willAnimateRotationToInterfaceOrientation:中创建按钮 只需从createButtonArray中提取按钮并修改其frame属性。

答案 1 :(得分:0)

代码行

chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];

创建UIButton类的新实例。您必须将该对象添加到视图以使其可见。在您的代码中,您还创建了太多UIButton自动释放的实例。

最好的办法是:

  1. 创建一个NSArray类型的实例变量,它将包含所有UIButton实例
  2. 通过[UIButton new][[UIButton alloc] init]
  3. 创建UIButton对象
  4. 在方法- (void)createButtonMatrix
  5. 中将所有UIButton对象添加到该数组中
  6. 将所有UIButton对象添加到视图
  7. 通过我们之前创建的数组
  8. 管理方法- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration中的UIButton对象的帧

答案 2 :(得分:0)

感谢art-divin,感谢Jean-Baptiste通过电子邮件提供答案。下面是在旋转时重新定位按钮矩阵的代码。创建按钮矩阵的代码保持不变。

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

if (orientation == UIInterfaceOrientationPortrait) {
    COLUMNS = 7;
    ROWSS = 8;

    int i=0; // button count iterator
    for (int rows = 0; rows < ROWSS; rows++) {
        for (int columns = 0; columns < COLUMNS; columns++) {
            NSUInteger chapter =  columns +  rows * COLUMNS + 1;
            if (chapter > 50)
                break;
            UIButton *b = (UIButton *)[call.createButtonArray objectAtIndex:i++];
            b.frame = CGRectMake(columns * WIDTH, rows * 38, HEIGHT, 37); // columns * X, rows * Y, width, height
        }
    }

    [rotateController rotateChapterButtons_Portrait];
}

else if (orientation == UIInterfaceOrientationLandscapeRight ||orientation == UIInterfaceOrientationLandscapeLeft) {
    COLUMNS = 10;
    ROWSS = 9;
    int i=0; // button count iterator
    for (int rows = 0; rows < ROWSS; rows++) {
        for (int columns = 0; columns < COLUMNS; columns++) {
            NSUInteger chapter =  columns +  rows * COLUMNS + 1;
            if (chapter > 50) // > greater than
                break;
            NSLog(@"%i:",chapter);
            UIButton *b = (UIButton *)[call.createButtonArray objectAtIndex:i++];
            b.frame = CGRectMake(columns * 48, rows * 38, 47, 37); // columns * X, rows * Y, width, height
        }
    }

    [rotateController rotateChapterButtons_Landscape];
}

}