简体   繁体   English

Swift-UIScrollView跳转到第一页而不是滚动到它

[英]Swift - UIScrollView Jumps to first page instead scrolling to it

Is there a way to add circle scrolling to UIScrollView? 有没有一种方法可以将圆形滚动添加到UIScrollView? For Example if the scroll reaches the last page the first page should be next and if scrolling back reaches the first page the last page should be next. 例如,如果滚动到达最后一页,则第一页应该是下一页,如果向后滚动到第一页,则最后一页应该是下一页。

I have been trying using scrollViewDidScroll function however, when the scroll reaches the last page the view jumps back to first view instead of scrolling to it or add it next to it. 我一直在尝试使用scrollViewDidScroll函数,但是,当滚动到达最后一页时,视图跳回到第一个视图,而不是滚动到它或在它旁边添加它。

My code is: 我的代码是:

func scrollViewDidScroll(scrollView: UIScrollView) {

        if (scrollView.contentOffset.x > scrollView.frame.size.width){
            scrollView.setContentOffset(CGPointZero, animated: true)
            //scrollView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: false)


        }

        if (scrollView.contentOffset.x < 0){
            scrollView.setContentOffset(CGPointMake(scrollView.frame.size.width,0.0), animated: true)
        }
    } 

Any Idea how to do this? 任何想法如何做到这一点?

You're going to have to do a small amount of around the back work but it's nothing too arduous. 您将需要在后勤工作中做少量的工作,但这并不太麻烦。

Seemingly displaying the first page of content to the right of the last isn't something that a scroll view can natively do. 似乎将内容的第一页显示在最后一页的右侧,这并不是滚动视图本来可以执行的操作。 To fix that, you need to extend your contentSize by an extra screen to the right and duplicate your content. 要解决此问题,您需要在右侧增加一个屏幕来扩展contentSize并复制您的内容。

That'll move you from having a content area that covers the range [0, end of last page) to [0, end of last page + 1). 这将使您的内容区域覆盖[0,最后一页末尾]到[0,最后一页末尾+ 1)。 But you still want scrolling to be constrained to [0, last page), with offsets in the range [-1, 0] being mapped to [last page, last page + 1). 但是,您仍然希望将滚动限制为[0,最后一页],将[-1,0]范围内的偏移量映射为[最后一页,最后一页+ 1]。 So: 所以:

func scrollViewDidScroll(scrollView: UIScrollView) {

    let realContentWidth = scrollView.contentSize.width - scrollView.bounds.size.width

    scrollView.contentOffset.x = ((scrollView.contentOffset.x % realContentWidth) 
                                                    + realContentWidth) % realContentWidth;
} 

The logic on that modulo is: 该模的逻辑是:

  • if you start with content offset being a positive number then the inner % will map it to the correct range and the outer will cause the + to be a no-op; 如果您以内容偏移量为正数开头,则内部的%会将其映射到正确的范围,而外部的将导致+为无操作; but
  • if you start with the content offset being a negative number then the inner % will leave it still negative but in (-realContentWidth, 0], so the + will then move it into the corresponding positive range with the outer % being a no-op. 如果以内容偏移量为负数开头,则内部%将使其仍为负数,但在(-realContentWidth,0]中,因此+将其移至相应的正数范围,外部%为无操作。

(with the quick observation for Objective-C'ers: % is defined on floating point numbers in Swift; in Objective-C you'd use fmod in its place as % is defined only for integral arithmetic there) (对Objective-C'ers的快速观察: %是在Swift中的浮点数上定义的;在Objective-C中,您应该在其位置使用fmod ,因为%仅在其中为积分算术定义了)

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

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