1

私のアプリケーションには、次のようなアイテムのグリッドがあります。

http://cl.ly/1m243117220B060Z0M26/Screen%20Shot%202012-07-09%20at%2011.34.22%20AM.png

これらは、現時点では単なるカスタムUIButtonです。これらのグリッドアイテムをランドスケープモードの幅に合わせて自動的に再配置するための最良の方法は何でしょうか。

http://cl.ly/1d0x431P1n211W1H2n3B/Screen%20Shot%202012-07-09%20at%2011.35.42%20AM.png

明らかに、これはアプリで一般的に行われていますが、少し検索したところ、探しているものが正確に見つかりませんでした。

iOS用Isotopeのようなものを誰かが知っている場合にボーナスを追加しました(http://isotope.metafizzy.co/

4

1 に答える 1

1

おそらく、スクロールビューのサイズに基づいてスクロールビューと画像(または私の場合は画像付きのボタン)を調整するルーチンが必要です(そしてautoSizingMask、方向が変わると伸びるようにスクロールビューを設定してください)。

したがって、たとえば、次のルーチンがあります(アイコンごとにUIButtonがすでに追加されたスクロールビューが作成されていることを前提としています...この基本的な考え方は、UIImageViewsを使用している場合にも機能します)。

- (void)rearrangeImages
{
    if (!_listOfImages)
    {
        [self loadImages];
        return;
    }

    // a few varibles to keep track of where I am

    int const imageWidth = [self thumbnailSize];
    int const imagesPerRow = self.view.frame.size.width / (imageWidth + 2);
    int const imageHeight = imageWidth;
    int const imagePadding = (self.view.frame.size.width - imageWidth*imagesPerRow) / (imagesPerRow + 1);
    int const cellWidth = imageWidth + imagePadding;
    int const cellHeight = imageHeight + imagePadding;

    NSInteger row;
    NSInteger column;
    NSInteger index;

    CGRect newFrame;

    // iterate through the buttons

    for (UIView *button in [_scrollView subviews])
    {
        index = [button tag];

        if ([button isKindOfClass:[UIButton class]] && index < [_listOfImages count])
        {
            // figure out where the button should go

            row = floor(index / imagesPerRow);
            column = index % imagesPerRow;
            newFrame = CGRectMake(column * cellWidth  + imagePadding, 
                                  row    * cellHeight, 
                                  imageWidth, 
                                  imageHeight);

            if (button.frame.origin.x != newFrame.origin.x || button.frame.origin.y != newFrame.origin.y)
                [button setFrame:newFrame];
        }
    }

    NSInteger numberOfRows = floor(([_listOfImages count] - 1) / imagesPerRow) + 1;

    [_scrollView setContentSize:CGSizeMake(self.view.frame.size.width, numberOfRows * cellHeight)];
}

次に、画面の向きが変わったときにアプリにこれを呼び出させます。

- (void)viewWillLayoutSubviews
{
    [self rearrangeImages];
}

iOS 5より前のバージョンをサポートしている場合は、次のようなものも必要になる場合があります。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    float iOSversion = [[[UIDevice currentDevice] systemVersion] floatValue];

    // we don't need to do this in iOS 5, because viewWillLayoutSubviews is automatically called

    if (iOSversion < 5.0)
        [self viewWillLayoutSubviews];
}
于 2012-07-09T18:22:49.187 に答える