Is it possible to add a drag to reload horizontally for a collection view?

风格不统一 提交于 2020-01-16 19:33:01

问题


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


来源:https://stackoverflow.com/questions/56614319/is-it-possible-to-add-a-drag-to-reload-horizontally-for-a-collection-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!