scrollViewDidEndDecelerating被调用以进行简单的触摸

时间:2011-11-30 23:32:39

标签: ios uiscrollview uiscrollviewdelegate

我正在实施 UIScrollViewDelegate 并用它做很多事情。但是,我刚刚发现了一个恼人的问题。

我希望 scrollViewWillBeginDecelerating:之后始终调用 scrollViewDidEndDecelerating:。但是,如果我只是触摸我的ScrollView(实际上我正在触摸scrollView中的按钮),则会调用 scrollViewDidEndDecelerating:并且不会调用 scrollViewWillBeginDecelerating:

那么当我只是按下UIScrollView中的按钮时,如何避免 scrollViewDidEndDecelerating:

谢谢!

2 个答案:

答案 0 :(得分:1)

创建名为buttonPressed或类似名称的成员BOOL,并在init:方法中将其初始化为false。

每当按下按钮时,将BOOL设置为true,然后执行以下检查:

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

答案 1 :(得分:1)

我遇到了同样的问题,我通过制作一个保存当前页面数量的变量来修复它,如果它们等于那么就将它与本地当前页面变量进行比较,然后不进行。

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
}
相关问题