2017-03-06 27 views
0

后我的影响:有图像之间的间距,同时滚动(如照片应用程序)是唯一可见的。保证金 - 斯威夫特版本

很多老OBJ-C答案建议延长屏幕外滚动视图的边界,使其页面更远,并且使这个屏幕外空间图像之间的差距。

为pagingEnabled文档状态:

如果此属性的值是YES,滚动视图上停止滚动视图的边界当用户滚动的 倍数。

因此,在试图更改倍数值时,我扩展了scrollView的宽度,并启用了左侧分页。然而,没有答案,我实现过去的差距页面 - 他们总是把它留在视图:

enter image description here

所以如果滚动宽度较长,为什么没有分页适当的距离?

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) 

回答

0

我最大的哑奏但...我有一个平等的宽度限制设置,我会忘记......这意味着通过了滚动分页被固定在上海华宽度的倍数。

不是一个真正的答案,但,是的。记住你的约束。

1

正如文档告诉你的,一个“页面”宽度是滚动视图的边界宽度。

所以我们可以说图像是100分宽。假设图像之间的空间为10点。然后:

  • 滚动视图的宽度必须是110点。

  • 的空间必须在每个图像的每一侧分布5分,这样的(假设我们有3个图像):

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

这将导致每个页面在100pt图像由用的空间,每边5分,共110点,滚动视图的宽度:

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

enter image description here

+0

感谢您的回复。我已经尽力实现这一点 - 也许你可以证明它是如何完成的。 – Tim

+0

添加了截屏视频,演示。每个图像视图都有一个边框,因此您可以看到它们之间确实存在差距。这是我的答案中所描述的_exactly_创建的。 – matt

+0

我看到它是你描述的,但请记住它不是我描述的问题。我所包含的代码是我的尝试,如果您提到它,它会很有帮助。 – Tim