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!
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;
}
}
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
}
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