0

Update:

I belive it may not be possible given the folowing line in apples documentation:

When the user drags the top of the scrollable content area downward

Found here.

Let me know if there is a way to do this.


I am trying to make it so that when the user swipe left (the way you swipe up in many apps with tableViews to reload) in a collection view it will show a loading icon and reload data (the reload data part I can handle myself).

How can I detect this so I can call a reloadData() method?

Note: I am using a UICollectionView which only has one column and x rows. At the first cell if the user swipes left it should show a loading icon and reload data.

I am looking for a way to detect the slide left intended to reload.

What I have tried:

    let refreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    viewDidLoadMethods()
    refreshControl.tintColor = .black
    refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    collectionView.addSubview(refreshControl)
    collectionView.alwaysBounceHorizontal = true

But this only works vertically.

  • 1
    Possible duplicate of [UIRefreshControl on UICollectionView only works if the collection fills the height of the container](https://stackoverflow.com/questions/14678727/uirefreshcontrol-on-uicollectionview-only-works-if-the-collection-fills-the-heig) – SwiftiSwift Jun 15 '19 at 21:34
  • @jsbeginnerNodeJS That question covers an issue, mine does not. I am looking for guidance on how to detect this... –  Jun 15 '19 at 21:35
  • Look at the answers. – SwiftiSwift Jun 15 '19 at 21:38
  • @jsbeginnerNodeJS all answers show how to do it verticaly –  Jun 15 '19 at 21:43

1 Answers1

1

I solved this problem with the following, but I should note that there is no default fucntionality like there is for vertical refresh:

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset
    let inset = scrollView.contentInset
    let y: CGFloat = offset.x - inset.left
    let reload_distance: CGFloat = -80

    if y < reload_distance{
        shouldReload = true
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if let _ = scrollView as? UICollectionView {

        currentlyScrolling = false

        if shouldReload {
            baseVC.showReloading()
            reloadCollectionView()
        }
    }
 }