theflarenet
theflarenet

Reputation: 656

Disable UIScrollView inertia (scrollViewDidEndDecelerating) Swift

Setup: I have a Scroll View that works great horizontally as well as a UISwipeGestureRecognizer that triggers a segue to another view when I swipe down.

Problem: If I am scrolling horizontally and I begin to swipe down (vertical scrolling disabled) WHILE the scrolling is decelerating, the swipe down action (segue) does not execute. It only works once the scrolling deceleration is complete.

enter image description here

Is there a way to disable the scroll deceleration (aka inertia) so that my swipe down gesture can be detected instantly? Perhaps there's a workaround to force UISwipeGestureRecognizer to be detected first?

Solutions in Swift are highly appreciated!

Upvotes: 2

Views: 4142

Answers (3)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

Swift 3:

To disable decelerating scroll you can try:

func scrollViewWillEndDragging (scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.memory = scrollView.contentOffset
}

or you change this value:

scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
// UIScrollViewDecelerationRateFast is the other param

About your gestureRecognizer interferences you can call this method to exclude touching from:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    // insert here views to exclude from gesture
    if touch.view!.isDescendantOfView(self.excludeThisView) {
        return false
    }
    return true
}

Upvotes: 1

wj2061
wj2061

Reputation: 6885

UIScrollView has a pinchGestureRecognizer and a panGestureRecognizer.If you have a UISwipeGestureRecognizer,the gesture will most likely be recognized as a UIPanGestureRecognizer.
You can add a dependence to solve the problem:

scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeGesture)

Upvotes: 1

user2640633
user2640633

Reputation:

did you look here? Deactivate UIScrollView decelerating

the answer

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{  
[scrollView setContentOffset:scrollView.contentOffset animated:YES];   
}

converted to swift is

func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
scrollView.setContentOffset(scrollView.contentOffset, animated: true)
}

Upvotes: 1

Related Questions