iPhone UIImageView horizo​​ntall在UIScrollView中滚动 - 中心图像?

时间:2012-01-25 14:37:26

标签: ios uiimageview scroll

我有根据屏幕大小下载和调整图像大小以适应图像全屏的方法。最大图像尺寸为(320x416)。现在,我有全屏UIScrollView,我在其中创建UIImageView,其中initWithFrame在屏幕宽度上移动了X,水平添加了UIScrollView空间。 只要我在UIScrollView [imgView setContentMode:UIViewContentModeLeft]中设置UIImageView,就没问题了。使用[imgView setContentMode:UIViewContentModeCenter]它会移动图像,每个循环计数,向右添加半屏,所以我只能看到第一个图像,只有当我在scrollView边界后面一点时才会看到第二个和第三个的一半。

这是我的代码,你能否有人踢我,我错过了什么?谢谢。 我把NSLog放在imgView initWithFrame:CGRectMake下但coords正常,X位置移动320像素并在scrollView中安排imgView就可以了。

- (void)setImages {

    int screenWidth = self.view.frame.size.width;
    int screenHeight = self.view.frame.size.height;

    // I know count of images here => imageIndex
    [mainView setContentSize:CGSizeMake(screenWidth * imageIndex, screenHeight)];

    int actualWidth = screenWidth;

    for (int index = 0; index < imageIndex; index++) {

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(actualWidth - screenWidth, 0, actualWidth, screenHeight)];

        ImageProcessing *impc = [[ImageProcessing alloc] init];
        UIImage *image = [UIImage imageWithData:[imagesNSDataArray objectAtIndex:index]];
        UIImage *resampled =  [impc processImageToWidthAndHeight:image :screenWidth :screenHeight];

        // [imgView setContentMode:UIViewContentModeCenter];
        [imgView setContentMode:UIViewContentModeLeft];
        imgView.image = resampled;

        [mainView addSubview:imgView];

        actualWidth = actualWidth + screenWidth;
    }
}

1 个答案:

答案 0 :(得分:2)

您正在混合视图宽度和水平位置。视图的宽度不同 - 您可以使它们更宽更宽。当您尝试将图像置于视图内部时,它会因为视图比您想象的更宽而移动。


int viewX = 0;

for (int index = 0; index < imageCount; index++) {
    CGRect frame = CGRectMake(viewX, 0, screenWidth, screenHeight);
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];

    [...]

    viewX += screenWidth;
}