为简单的触摸而调用scrollViewDidEndDecelerating

发布于 12-19 07:02 字数 438 浏览 4 评论 0原文

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

我希望 scrollViewDidEndDecelerating: 始终在 scrollViewWillBeginDecelerating: 之后调用。但是,如果我只是触摸我的 ScrollView(实际上我触摸的是滚动视图内的按钮),则 scrollViewDidEndDecelerating: 会被调用,而 scrollViewWillBeginDecelerating: 不会被调用。

那么,当我只需按下 UIScrollView 内的按钮时,如何避免调用 scrollViewDidEndDecelerate: 呢?

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁2024-12-26 07:02:44

创建一个名为 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;
    }
}

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;
    }
}
白云悠悠2024-12-26 07:02:44

我遇到了同样的问题,我通过创建一个保存当前页面编号的变量并将其与本地当前页面变量进行比较(如果它们相等则不继续)来修复它。

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文