Is it possible to add a drag to reload horizontall

2019-12-16 18:31发布

问题:

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:

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()
        }
    }
 }