I'm using an UITableView and a UICollectionView on the same View Controller.
I wanted to change how the UICollectionView scrolls, so I added a scrollViewWillBeginDragging
and a scrollViewWillEndDragging
function inside the extension.
However, scrollViewWillBeginDragging
and a scrollViewWillEndDragging
is also affecting the UITableView, despite not being in the same extension.
How do I fix this? Is there a way to only select the UICollectionView?
Here is a short version of what my code looks like:
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// This is where I put all of the UICollectionView code, including `scrollViewWillBeginDragging` and a `scrollViewWillEndDragging`
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
// Why is the code in here affecting the UITableView?
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Same as with `scrollViewWillBeginDragging`
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
// This is where I put all of the UITableView code, it's separate from the UICollectionView so why are `scrollViewWillBeginDragging` and `scrollViewWillEndDragging` affecting it?
}
This is happening because
UITableViewDelegate and UICollectionViewDelegate both Inherits From
UIScrollViewDelegate
So what you can do is
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// This is where I put all of the UICollectionView code, including `scrollViewWillBeginDragging` and a `scrollViewWillEndDragging`
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView is UICollectionView {
// Add code here for collectionView
}
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView is UICollectionView {
// Add code here for collectionView
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With