1

我的应用程序中有一个项目网格,如下所示:

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

目前这些只是自定义 UIButtons。使这些网格项目自动重新排列以适应横向模式的宽度的最佳方法可能是什么?

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

显然,这通常在应用程序中完成,但我搜索了一下,找不到我想要的东西。

如果有人知道 Isotope for iOS ( 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 回答