Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollViewDidEndDecelerating being called for a simple touch

I'm implementing UIScrollViewDelegate and doing a lot of stuff with it. However, I just found an annoying issue.

I expect scrollViewDidEndDecelerating: to be called always after scrollViewWillBeginDecelerating:. However, if I simply touch my ScrollView (actually I'm touching a button inside the scrollView), scrollViewDidEndDecelerating: gets called and scrollViewWillBeginDecelerating: was not called.

So how can I avoid scrollViewDidEndDecelerating: being called when I simply press a button inside my UIScrollView?

Thank you!

like image 869
LocoMike Avatar asked Nov 30 '11 23:11

LocoMike


2 Answers

Create a member BOOL called buttonPressed or similar and initialise this to false in your init: method.

Set the BOOL to true whenever your button/s are hit, and then perform the following check:

-(void)scrollViewDidEndDecelerating: (UIScrollView*)scrollView
{
    if (!buttonPressed)
    {
        // resume normal processing
    }
    else
    {
        // you will need to decide on the best place to reset this variable.
        buttonPressed = NO;
    }
}
like image 103
Luke Avatar answered Nov 15 '22 15:11

Luke


I had the same problem I fixed it by making a variable that hold the current page num and compare it with the local current page variable if they r equal then don't proceed.

var currentPage : CGFloat = 0.0
var oldPage : CGFloat = 0.0

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
    // Test the offset and calculate the current page after scrolling ends

    let pageWidth:CGFloat = scrollView.frame.width
    let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
    // Change the indicator
    print("that's the self.currentPage \(self.currentPage) and that's the current : \(currentPage)")

    guard  self.currentPage != currentPage else { return }
    oldPage = self.currentPage
    self.currentPage = currentPage;

    print("that's the old \(oldPage) and that's the current : \(currentPage)")

       //Do something
}
like image 37
killvak Avatar answered Nov 15 '22 15:11

killvak