Reputation: 8589
I have a table view that doesn't cover the whole screen (It's kind of like a drawer from the bottom of the screen). When the user scrolls down to the end of the content I want to stop the scrolling and then add a pan gesture recognizer. I do this like so:
// MARK: UIScrollViewDelegate Methods
extension TutorProfileVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Limit top vert bounce
guard mode == .drawer else { return }
if scrollView.contentOffset.y < -80.0 {
scrollView.contentOffset = CGPoint(x: 0, y: -80.0)
tableView.addGestureRecognizer(tablePanGR)
}
}
}
The gesture has been added but won't register until the user touches the screen again. Their finger is already on the tableview. Is it possible to start the gesture without them having to touch the screen again?
Upvotes: 2
Views: 487
Reputation: 12144
I think you have same problem with this question. Take a look at it if you want to see a code sample.
To resolve problem, you should add gesture from beginning but only handle gesture action when user scrolls to bottom. So you don't need to touch the screen again because gesture is started when you begin scrolling. The method to handle gesture will look like below
@objc func handlePanGestureRecognizer(_ gestureRecognizer: UIPanGestureRecognizer) {
switch gestureRecognizer.state {
case .began:
// Do nothing
break
case .changed:
let translation = gestureRecognizer.translation(in: gestureRecognizer.view!.superview!)
let velocity = gestureRecognizer.velocity(in: gestureRecognizer.view!.superview)
let state = gestureRecognizer.state
// Don't do anything until |scrollView| reached bottom
if scrollView.contentOffset.y >= -80.0 {
return;
}
// Do whatever you want with |scrollView|
}
break;
case .cancelled:
case .ended:
case .failed:
default:
break;
}
}
Also implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
to make gesture and scroll view work together
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Upvotes: 1