简体   繁体   English

如何将UIPanGestureRecognizer添加到UIScrollView子视图

[英]How to add UIPanGestureRecognizer to UIScrollView subviews

I am adding subviews to a UIScrollView and then I add UIPanGestureRecognizer to these subviews. 我将子视图添加到UIScrollView ,然后我将UIPanGestureRecognizer添加到这些子视图。 Everything works fine but now after adding UIPanGestureRecognizer to subviews of the scroll view, scrolling is not possible. 一切正常但现在将UIPanGestureRecognizer添加到滚动视图的子视图后,无法滚动。

What can be the possible solution to this issue? 这个问题的可能解决方案是什么?

The problem is that the pan gesture recognizer is what is used in the scroll view to control scrolling. 问题是平移手势识别器是滚动视图中用于控制滚动的内容。 Your gesture recogniser is taking priority and disabling the scroll views 您的手势识别器正在优先处理并禁用滚动视图

If you want to always be able to scroll you can set your gesture recogniser to require the scroll views one to fail before it will work: 如果您希望始终能够滚动,则可以将手势识别器设置为要求滚动视图在其工作之前失败:

[self.myCustomPanRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer]; 

Edit: as Bastian pointed out in the comments, the reference to pan guesture is only in iOS 5, prior to this, check the array of gesture recognisers and find the one of type UIPanGestureRecognizer 编辑:正如Bastian在评论中指出的那样,对于pan guest的引用仅在iOS 5中,在此之前,检查手势识别器的数组并找到UIPanGestureRecognizer类型之一

if you want both to work you may need to do something to separate your recogniser from the scroll views, eg make the user tap and hold before your custom recogniser will be recognised. 如果您希望两者兼顾,则可能需要执行一些操作以将识别器与滚动视图分开,例如,在识别自定义识别器之前,让用户点按并按住。

there is also a delegate method which will allow both recognisers to work together, but I'm not sure how well this works when both are the same type 还有一个委托方法,允许两个识别器一起工作,但我不确定这两种方法是否相同

If you want to use both simultaneus you can use 如果你想同时使用两者,你​​可以使用

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

from the delegate, but that is probably not what you want ;) 来自代表,但这可能不是你想要的;)

Swift 4 斯威夫特4

Conform the view controller to the UIGestureRecognizerDelegate ... 使视图控制器符合UIGestureRecognizerDelegate ...

SomeViewController: UIViewController, UIGestureRecognizerDelegate {
    ...
}

...set the view controller as the custom pan gesture recognizer's delegate... ...将视图控制器设置为自定义平移手势识别器的委托...

customPanGestureRecognizer.delegate = self

...and using the simultaneous delegate, allow the custom panner and the scroll view's (or table view's) panner to operate simultaneously... ...并使用同时委托,允许自定义平移器和滚动视图(或表视图)的平移器同时操作...

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    if otherGestureRecognizer == scrollView.panGestureRecognizer { // or tableView.panGestureRecognizer
        return true
    } else {
        return false
    }

}

There are two other methods that ask the delegate if a gesture recognizer should require another gesture recognizer to fail or if a gesture recognizer should be required to fail by another gesture recognizer. 还有另外两种方法询问代表是否手势识别器应该要求另一个手势识别器失败,或者是否应该要求手势识别器通过另一个手势识别器失败。 You will likely need further optimization beyond this but this is the starting point. 您可能需要进一步优化,但这是起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM