【问题标题】:Long Press Gesture Recognizer Interfering With Scroll in UITableView长按手势识别器干扰 UITableView 中的滚动
【发布时间】:2015-06-05 10:19:42
【问题描述】:

我在向 UITableView 添加长按手势时遇到问题。好吧,从技术上讲,我确实有一个长按手势识别器,但我将最小点击持续时间设置为 0.08。我这样做是因为我希望对点击和按住一个单元格有相同的一般操作,但该操作仅根据单元格被按住的时间而改变。无论如何,这是我添加手势识别器的代码(viewDidLoad):

    var longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    longPress.minimumPressDuration = 0.08
    longPress.delegate = self
    longPress.cancelsTouchesInView  = false
    self.tableView.addGestureRecognizer(longPress)
    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(longPress)

在我的handleLongPress() 函数中,我得到长按的CGPoint,然后从中得到tableView 单元格。

所以基本上,如果我快速滚动,(就像我轻弹屏幕),表格视图滚动得很好。如果我尝试缓慢滚动,则会触发长按事件并且我无法滚动。

我想要做的就是能够缓慢滚动,我希望 tableviews 默认滚动手势覆盖任何长按。

谢谢!

【问题讨论】:

    标签: ios objective-c uitableview swift uigesturerecognizer


    【解决方案1】:

    ScrollViews 有一个panGestureRecognizer 属性,您可以在长按识别器上调用requireGestureRecognizerToFail,并将scrollView 的panGestureRecognizer 作为参数,并且它只会在滚动视图中的平移手势失败时触发。

    【讨论】:

    • 这解决了问题,但现在无论如何长按都失败了。任何想法如何解决这个问题?
    • 也许你可以反过来做,告诉滚动视图的识别器只有在长按失败时才触发。
    【解决方案2】:

    这个类别可能会解决您的问题:

    @interface UITableView (CellSwipeAdditions)
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
    @end
    
    @implementation UITableView (CellSwipeAdditions)
        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
            return YES;
        }
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-13
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多