拖动按钮时ScrollView不滚动

时间:2023-01-23 16:56:47

I have a scroll view that used to scroll when it didn't have buttons all over it. Now it does, and when dragging the mouse (on simulator) nothing happens (i think because the buttons are being pushed). How can I make this right?

我有一个滚动视图,用于滚动它没有全部按钮。现在它确实如此,当拖动鼠标时(在模拟器上)没有任何反应(我认为因为按钮被推动)。我怎么能做到这一点?

4 个解决方案

#1


94  

This is happening because UIButton subviews of the UIScrollView (I assume buttons are added as subviews in your case) are tracking the touches and not the scroll view. UIScrollView method touchesShouldCancelInContentView is the key here. According to its description: "The default returned value is YES if view is not a UIControl object; otherwise, it returns NO.", i.e. for UIControl objects (buttons), UIScrollView does not attempt to cancel touches which prevents scrolling.

发生这种情况是因为UIScrollView的UIButton子视图(我假设按钮在您的情况下被添加为子视图)正在跟踪触摸而不是滚动视图。 UIScrollView方法touchesShouldCancelInContentView是这里的关键。根据其描述:“如果视图不是UIControl对象,则默认返回值为YES;否则返回NO。”,即对于UIControl对象(按钮),UIScrollView不会尝试取消阻止滚动的触摸。

So, to allow scrolling with buttons:

因此,允许滚动按钮:

  1. Make sure UIScrollView property canCancelContentTouches is set to YES.
  2. 确保UIScrollView属性canCancelContentTouches设置为YES。
  3. Subclass UIScrollView and override touchesShouldCancelInContentView to return YES when content view object is a UIButton, like this:
  4. 子类UIScrollView并覆盖touchesShouldCancelInContentView,当内容视图对象是UIButton时返回YES,如下所示:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    if ( [view isKindOfClass:[UIButton class]] ) {
        return YES;
    }

    return [super touchesShouldCancelInContentView:view];
}

#2


9  

I founded this question looking for the swift solution for this problem, I "translated" it like this:

我创建了这个问题寻找这个问题的快速解决方案,我“翻译”它像这样:

class UIButtonScrollView: UIScrollView {

    override func touchesShouldCancelInContentView(view: UIView!) -> Bool {
        if (view.isKindOfClass(UIButton)) {
            return true
        }

        return super.touchesShouldCancelInContentView(view)

    }

}

hope this could help

希望这可以帮助

#3


5  

Swift 3 Solution

Swift 3解决方案

override func touchesShouldCancel(in view: UIView) -> Bool {
    if view is UIButton {
        return true
    }
    return super.touchesShouldCancel(in: view)
}

#4


0  

One thing to try if you're seeing this in a simulator is to run on an actual phone. I couldn't scroll in the simulator but no prob on my phone.

如果您在模拟器中看到这一点,那么尝试的一件事就是在实际的手机上运行。我无法在模拟器中滚动但在我的手机上没有问题。

#1


94  

This is happening because UIButton subviews of the UIScrollView (I assume buttons are added as subviews in your case) are tracking the touches and not the scroll view. UIScrollView method touchesShouldCancelInContentView is the key here. According to its description: "The default returned value is YES if view is not a UIControl object; otherwise, it returns NO.", i.e. for UIControl objects (buttons), UIScrollView does not attempt to cancel touches which prevents scrolling.

发生这种情况是因为UIScrollView的UIButton子视图(我假设按钮在您的情况下被添加为子视图)正在跟踪触摸而不是滚动视图。 UIScrollView方法touchesShouldCancelInContentView是这里的关键。根据其描述:“如果视图不是UIControl对象,则默认返回值为YES;否则返回NO。”,即对于UIControl对象(按钮),UIScrollView不会尝试取消阻止滚动的触摸。

So, to allow scrolling with buttons:

因此,允许滚动按钮:

  1. Make sure UIScrollView property canCancelContentTouches is set to YES.
  2. 确保UIScrollView属性canCancelContentTouches设置为YES。
  3. Subclass UIScrollView and override touchesShouldCancelInContentView to return YES when content view object is a UIButton, like this:
  4. 子类UIScrollView并覆盖touchesShouldCancelInContentView,当内容视图对象是UIButton时返回YES,如下所示:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    if ( [view isKindOfClass:[UIButton class]] ) {
        return YES;
    }

    return [super touchesShouldCancelInContentView:view];
}

#2


9  

I founded this question looking for the swift solution for this problem, I "translated" it like this:

我创建了这个问题寻找这个问题的快速解决方案,我“翻译”它像这样:

class UIButtonScrollView: UIScrollView {

    override func touchesShouldCancelInContentView(view: UIView!) -> Bool {
        if (view.isKindOfClass(UIButton)) {
            return true
        }

        return super.touchesShouldCancelInContentView(view)

    }

}

hope this could help

希望这可以帮助

#3


5  

Swift 3 Solution

Swift 3解决方案

override func touchesShouldCancel(in view: UIView) -> Bool {
    if view is UIButton {
        return true
    }
    return super.touchesShouldCancel(in: view)
}

#4


0  

One thing to try if you're seeing this in a simulator is to run on an actual phone. I couldn't scroll in the simulator but no prob on my phone.

如果您在模拟器中看到这一点,那么尝试的一件事就是在实际的手机上运行。我无法在模拟器中滚动但在我的手机上没有问题。