ios - UICollectionViewCell 和 UICollectionView 中的平移手势

标签 ios swift uicollectionview uicollectionviewcell uipangesturerecognizer

我有一个可以上下滑动的栏的 Collection View 。 Collection View 中的每个单元格都使用 UIPanGestureRecognizer 来控制蓝色条上下滑动。 Collection View 不会在此处滚动。

Active

有一个“编辑模式”,可以禁用控制栏的平移手势。这里的希望是,在“编辑模式”下, Collection View 可以左右滚动。

Edit Mode

我尝试这样做是禁用每个单元格中的平移手势。我还尝试使用 UIGestureRecognizerDelegate 方法来尝试禁用触摸并使单元格的平移手势失败,以支持 Collection View 的平移手势。 Collection View 的平移手势似乎没有转发到任何单元格的手势委托(delegate)调用。

这些委托(delegate)调用让我最接近(单元格平移手势的委托(delegate)):

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    return !self.editMode  // bar doesn't need to pan in edit mode
}

实现此功能后,如果我在单元格之间的空白区域开始平移,我就可以平移 Collection View 。不过,在单元格上启动平底锅不会产生任何作用。

编辑:我上传了 sample project将问题发送到 github。

最佳答案

如果我理解正确的话,我认为问题可能如下:

  1. 您的 UICollectionView 将监听平移手势。到目前为止一切顺利。
  2. 在 KBHBarSlider 中,您为 slider 添加 UIPanGestureRecognizer。它们比底层 UICollectionView 具有“优先级”。

这实际上是有道理的,因为您的 BarSlider 位于 CollectionView 之上。我最好的做法是让 BarSlider 在需要时添加 UIPanGestureRecognizer,并在编辑时将它们完全删除。

因此,首先尝试删除您在 VOLBarSliderCell 中添加的 gestureRecognizer

然后在 KBHBarSlider 中添加如下内容:(并确保您的 init 调用 addPanRecognizer() 而不是 setup())

func addPanRecognizer() {
    let panGesture = UIPanGestureRecognizer(target: self, action: "viewDidPan:")
    self.gestureRecognizers = [panGesture]
}

func removePanRecognizer() {
  self.gestureRecognizers = []
}

现在可以根据需要添加和删除手势,从 VOLBarSliderCell 中的编辑获取/设置覆盖中执行此操作:

var editing: Bool = false {
    didSet {            
        if !self.editing {
            self.barSlider.addPanRecognizer()
            // Removed code, to keep this example short...
        } else {
            self.barSlider.removePanRecognizer()
            // Removed code, to keep this example short...
        }
    }
}

关于ios - UICollectionViewCell 和 UICollectionView 中的平移手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448269/

相关文章:

ios - XCode 7 - Swift - 通过 SFSafariViewController 自动加载站点

ios - 完成处理程序如何在 iOS 上工作?

ios - 在 iOS 上设置 Firebase 通知时出错

ios - 如何使用 Firebase 更改我的值

swift - 为 map swift 设置新区域时出错

Iphone - 从 viewController 之外的另一个类调用动画到 View

ios - initWithNibName : @"-VS- self.storyboard instantiateViewControllerWithIdentifier:@"?

ios - UICollectionView 在保持位置上方插入单元格(如 Messages.app)

ios - 在 iOS 上创建多列 View

ios - 在交互过渡期间更改 UICollectionViewCell 的内容