iOS 9 中的 UITapGestureRecognizer 问题

UITapGestureRecognizer issue in iOS 9

伙计们,

今天,我已将我的 iPhone 更新为 iOS 9,现在手势识别器出现问题。 这是错误:

WARNING: A Gesture recognizer (; target= <(action=onVideoTap:, target=)>>) was setup in a storyboard/xib to be added to more than one view (->; layer = >) at a time, this was never allowed, and is now enforced. Beginning with iOS 9.0 it will be put in the first view it is loaded into.

我在使用 iOS8 时没有这个问题。 该视图包含一个 UIImageView 和一个 TextView。识别器已放入 ImageView 中,并且仅引用此视图的出口。

这个问题我不是很懂。 有人可以帮我吗?谢谢:)

已经修复了。 情节提要是本地化的,我用一种语言将识别器分配给图片视图两次。 不知何故,这似乎也给其他故事板带来了麻烦。

这件事发生在我身上,因为我想使用 Tap Gesture RecognizeTableView 中包含的 TableViewCell 中的图像。

问题在于:

我添加了一个 Tap Gesture Recognizer 但我的 table.[=27= 中有多个 TableViewCell(不止一个图像) ]

iOS会将UITapGestureRecognizer分配给第一个单元格中的第一个图像,其他单元格将没有手势(手势已设置为仅第一个图像)。

要解决此问题,请执行以下操作:

  1. 确保您勾选了 User Interaction Enabled 以获得您想要分配 TapRecognizerGestureUIView
  2. 在子视图 TableViewCell 中添加一个新的 UITapGestureRecognizer。代码:

    internal let tapRecognizer1: UITapGestureRecognizer = UITapGestureRecognizer()`
    
  3. 在你的主视图中 TableView 在我的例子中,对于每个单元格,将你对每个单元格所做的 UITapGestureRecognizer 分配给主视图中的主函数:

    cell.tapRecognizer1.addTarget(self, action: "img_Click:")
    cell.img.gestureRecognizers = []
    cell.img.gestureRecognizers!.append(cell.tapRecognizer1)
    
  4. 添加您希望 UITapGestureRecognizer 在单击时触发的功能:

    func img_Click(sender: UITapGestureRecognizer) {
        print("ok")
    }
    

备注:

  • 如果您不想在主视图中执行 UITapGestureRecognizer 操作,可以使用简单的方法,直接在其子视图中分配它。
  • 第 4 步中的函数名称必须与 addTarget 行中的相同。

我认为当您使用故事板添加了点击手势识别器时会发生此问题。因为某些原因你添加了多个视图。(见图片)。

所以,删除其他错误的观点,留下正确的观点。