如何为不同的组件使用scrollViewWillEndDragging?

时间:2018-12-17 21:52:45

标签: swift uitableview uicollectionview uikit

我在CollectionView中有TabelViewCell,并且我正在尝试对CollectionView的单元进行分页和居中。但是当我使用方法

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){}

它适用于TabelView和CollectionView。我知道这些类型是从UIScrollView继承的,所以才出现这种情况。要将该方法仅应用于一种类型,需要做些什么?

1 个答案:

答案 0 :(得分:0)

您可以通过检查scrollView的类型来做到这一点:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if scrollView is UITableView {
        // Do your thing for the tableView.
    } else if scrollView is UICollectionView {
        // Do your thing for the collectionView.
    }
}