在UIScrollView中添加图像之间的间距

jar*_*ryd 6 iphone

如何在添加到UIScrollView的多个图像之间添加间隙/空格?

一个例子是照片应用程序,在分页时图像之间有黑色间距.

zrz*_*zka 21

我假设您知道如何在启用分页的情况下将图像添加到UIScrollView.我还假设您想要查看全屏照片,但在滚动时,您希望它们之间存在差距.如果是,这是可能的解决方案之一......

  • 将差距定义为20px
  • UIScrollView是全屏视图,320x480适用于iPhone
  • 将UIScrollView框架设置为CGRectMake(0,0,340,480); // 340 = 320 + 20(间隙,这些20px在屏幕外)
  • 将第一个图像帧设置为CGRectMake(0,0,320,480);
  • 第二个图像帧到CGRectMake(340,0,320,480);
  • ...我的图像CGRectMake(340*I,0,320,480)//我是索引 - 0..N-1
  • 将UIScrollView内容大小设置为CGSizeMake(340*N,480)// N =图像数量

...如果您不想进行缩放,只需全屏,分页,滚动间隙,这是非常快速的解决方案.


Nyx*_*0uf 5

这只是基础数学..

您将UIImageView添加为子视图,并计算您想知道子视图大小和scrollView的contentSize的空间.

编辑:

这基本上建立了一个画廊

-(void)photosGallery
{
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 416.0f}];
    scrollView.contentSize = (CGSize){.width = view.frame.size.width, .height = 372.0f};
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    /// Build gallery
    CGSize thumbSize = (CGSize){.width = 75.0f, .height = 75.0f};
    const CGFloat xSpace = (scrollView.frame.size.width - (thumbSize.width * kPictsPerRow)) / (kPictsPerRow + 1); // kPictsPerRow = Number of pictures in a row
    const CGFloat xInc = thumbSize.width + xSpace;
    const CGFloat yInc = thumbSize.height + 15.0f;
    CGFloat x = xSpace, y = 10.0f;
    for (NSUInteger i = kMaxPictures; i--;)
    {
        UIImageView* pv = [[UIImageView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = thumbSize}];
        [scrollView addSubview:pv];
        [pv release];
        x += xInc;
        const NSUInteger eor = i % kPictsPerRow;

        if (!eor)
            y += yInc, x = xSpace;
    }
    [scrollView release];
}
Run Code Online (Sandbox Code Playgroud)