简体   繁体   English

UIScrollView中图像之间的边距

[英]Margin between images in UIScrollView

The effect I'm after: Having spacing between images that is only visible while scrolling (like the photos app). 我要的效果是:图像之间只有在滚动时才可见的间隔(例如photos应用程序)。

A lot of old obj-c answers suggest extending the scroll view's bounds offscreen to make it page farther, and making this offscreen space the gap between images. 许多旧的obj-c答案都建议在屏幕外扩展滚动视图的边界,以使页面更远,并使屏幕外空间成为图像之间的间隙。

The documentation for pagingEnabled states: pageEnabled的文档指出:

If the value of this property is YES, the scroll view stops on multiples of the scroll view's bounds when the user scrolls. 如果此属性的值为YES,则当用户滚动时,滚动视图会在滚动视图的边界的倍数处停止。

So in trying to change the multiples value, I extended the scrollView's width, and left paging enabled. 因此,在尝试更改倍数值时,我扩展了scrollView的宽度,并启用了分页。 Yet no answers I implement page past the gap - they always leave it in view: 但是,我没有实现任何解决方案,都无法解决问题-他们总是将其留在视野中:

在此处输入图片说明

So if the scroll width is longer, why isn't it paging the proper distance? 因此,如果滚动宽度较长,为什么它不分页正确的距离呢?

    let gapMargin = CGFloat(20)
    scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width + gapMargin, height: view.frame.height)
    let exdScrollWidth = scrollView.frame.width

    //1
    let imageView1 = UIImageView()
    imageView1.backgroundColor = UIColor.green
    imageView1.frame = CGRect(x: 0, y: 0, width: exdScrollWidth - gapMargin, height: scrollView.bounds.size.height)

    //2
    let imageView2 = UIImageView()
    imageView2.backgroundColor = UIColor.yellow
    imageView2.frame = CGRect(x: exdScrollWidth, y: 0, width: exdScrollWidth - gapMargin, height: scrollView.bounds.size.height)

    //3
    let imageView3 = UIImageView()
    imageView3.backgroundColor = UIColor.red
    imageView3.frame = CGRect(x: exdScrollWidth * 2, y: 0, width: exdScrollWidth - gapMargin, height: scrollView.bounds.size.height)

    scrollView.contentSize.width = exdScrollWidth * 3

    scrollView.addSubview(imageView1)
    scrollView.addSubview(imageView2)
    scrollView.addSubview(imageView3)

As the docs tell you, one "page" width is the bounds width of the scroll view. 正如文档所告诉的那样,一个“页面”宽度是滚动视图的边界宽度。

So let's say the images are 100 points wide. 因此,假设图片的宽度为100点。 And let's say the space between the images is to be 10 points. 假设图片之间的间隔为10点。 Then: 然后:

  • The scroll view's width must be 110 points. 滚动视图的宽度必须为110点。

  • The spaces must be distributed 5 points on each side of each image, like this (supposing we have 3 images): 空格必须在每张图片的每一侧分配5个点,如下所示(假设我们有3张图片):

     5pts - 100pts (im) - 10pts - 100pts (im) - 10pts - 100pts(im) - 5pts 

This will cause each page to consist in a 100pt image with 5 pts of space on each side, a total of 110 pts, the width of the scroll view: 这将导致每个页面包含一个100pt的图像,每侧上有5 pts的空间,总共110 pts,即滚动视图的宽度:

    5pts - 100pts (im) - 10pts - 100pts (im) - 10pts - 100pts(im) - 5pts
    |                      |                      |                    |

在此处输入图片说明

It turns out I had an equal widths constraint set up that I'd forgotten about. 事实证明,我设置了一个相等的宽度约束,而我忘记了。 This meant the multiple by which the scrollview paged was fixed at the width of the superview. 这意味着滚动视图分页的倍数固定为超级视图的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM