2

When the offset sits at 0, there is no need for the user to swipe left as there is nothing to see in that direction with the following code:-

collectionView.contentOffset.x = max(0,scrollView.contentOffset.x)

But what about disabling swiping right on the most rightView?

Pushp
  • 1,064
  • 10
  • 18

3 Answers3

0

I'm not 100% I understand your question, but is this what you're looking for?

collectionView.contentOffset.x = max(min(collectionView.contentSize.width - collectionView.bounds.width, collectionView.contentOffset.x), 0)

If it is, also make sure the contentSize is larger than the collectionView's bounds.

Zoltan Vari
  • 156
  • 4
  • actually, I am not looking for this. My question is: When you are at the right end, if you still try to scroll, there is a white page appearing. Im trying to block scroll on the last page so I can block that white area. –  Nov 03 '18 at 08:09
0

use this implemente uiscrollviewdelegate

 override func scrollViewDidScroll(_ scrollView: UIScrollView) {


        var bottomEdge:CGFloat = scrollView.contentOffset.x + scrollView.frame.size.width;

        if (bottomEdge == scrollView.contentSize.width)
        {
            // we are at the bottom
            self.collectionView.isScrollEnabled = false
            self.collectionView.isPagingEnabled = false
        } else {
            self.collectionView.isScrollEnabled = true
            self.collectionView.isPagingEnabled = true
        }

    }
Pushp
  • 1,064
  • 10
  • 18
  • Thank you for your time but the code that you shared is checking if its the right end or not, but when you scroll to right end, its presenting the left end(first one) –  Nov 03 '18 at 08:08
  • yes thats why i puted a comment do whatever you want i put just scroll to left – Pushp Nov 03 '18 at 08:09
  • you can disable intraction of collectionview – Pushp Nov 03 '18 at 08:10
  • I am trying to block scrolling to right when im at the right end. When you are at the right end, if you still try to scroll, there is a white page appearing. Im trying to block scroll on the last page so I can block that white area –  Nov 03 '18 at 08:10
  • have you using paging in collectionview? – Pushp Nov 03 '18 at 08:11
  • yes, I have a pageController. And I have only 3 pages. So when im on the 3rd page, I don't want users to scroll right anymore –  Nov 03 '18 at 08:12
  • https://stackoverflow.com/a/44614770/5725512 you can find the last page using this – Pushp Nov 03 '18 at 08:15
  • and after that you can disable userintration – Pushp Nov 03 '18 at 08:15
  • I can find the last page so easily, but I don't want to disable user interaction. Users might want to scroll back to previous one. So I only want to disable scroll to next one –  Nov 03 '18 at 08:17
  • Please check updated answer. if still need some improvement let eloborate – Pushp Nov 03 '18 at 08:39
0

Have you tried this?

collectionView.bounces = false
Zoltan Vari
  • 156
  • 4