UIScrollView中图像之间的边距 [英] Margin between images in UIScrollView
问题描述
我想要的效果:图像之间只有在滚动时才可见的间隔(如photos应用程序).
The effect I'm after: Having spacing between images that is only visible while scrolling (like the photos app).
许多旧的obj-c答案都建议在屏幕外扩展滚动视图的边界以使其分页更远,并使屏幕外空间在图像之间形成间隙.
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.
pagingEnabled状态的文档:
The documentation for pagingEnabled states:
如果此属性的值为YES,则滚动视图将在以下位置停止 用户滚动时滚动视图边界的倍数.
If the value of this property is YES, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls.
因此,在尝试更改倍数值时,我扩展了scrollView的宽度,并启用了分页.但是我没有答案能解决这个问题-他们总是把它留在眼前:
So in trying to change the multiples value, I extended the scrollView's width, and left paging enabled. 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)
推荐答案
事实证明,我设置了一个相等的宽度约束,而我忘记了.这意味着滚动视图分页的倍数固定为超级视图的宽度.
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.
这篇关于UIScrollView中图像之间的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!