添加到 collectionview 单元格中的 uiview 时,点击手势未按预期工作 [英] Tap Gesture not working as expected when added to uiview in collectionview cell

查看:26
本文介绍了添加到 collectionview 单元格中的 uiview 时,点击手势未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式向自定义集合视图单元格类添加点击手势识别器.出于某种原因,它似乎不起作用.框架不是 0,isUserInteractionEnabled 设置为 true,我确保点击视图位于所有其他视图之上:

I am adding a tap gesture recognizer programmatically to a custom collection view cell class. for some reason, it doesnt seem to be working. the frame is not 0, isUserInteractionEnabled is set to true and i made sure the tap view is on top of all other views:

自定义单元格类:

let containerView: UIView = {
    let view = UIView()
    view.isUserInteractionEnabled = true
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

let tapView: UIView = {
    let v = UIView()
    v.isUserInteractionEnabled = true
    return v
}()

let tap: UITapGestureRecognizer = {
    let t = UITapGestureRecognizer(target: self, action: #selector(tapped))
    return t
}()

@objc fileprivate func tapped() {
    print("tap")
}

func setTap() {
    self.containerView.addSubview(tapView)
    tapView.frame = self.frame
    // layout constraint code - printing frame shows its not 0 after this

    tapView.addGestureRecognizer(tap)
}

在具有集合视图的视图控制器文件中:

In the view controller file that has the collection view:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SuggestCell
    cell.category.text = data[indexPath.row].category
    cell.word.text = data[indexPath.row].word
    cell.setTap()
    print(cell.tapView.frame)
    return cell
}

我确实意识到有一个 didSelectItemAt 方法,但我正在尝试一些自定义行为来检测对单元格的多次点击并执行操作

I do realize that there is a didSelectItemAt method but I am trying some custom behavior that detects multiple taps to a cell and performs an action

推荐答案

问题是在你定义的 tap 属性中,self 不是自定义的实例cell 类,因为在创建属性时,对象还没有完全初始化.

The problem is that in your definition of tap property, self is not the instance of the custom cell class because at the time the property is created, the object hasn't been fully initialized.

如果添加:

print(type(of: self))

到那个代码,你会看到它打印:

to that code, you will see that it prints:

(CustomCell) -> () -> CustomCell

而不是想要的:

CustomCell

所以你的目标/操作使用了错误的目标.

So your target/action is using the wrong target.

解决这个问题的一个简单方法是使 tap 成为一个 lazy var:

An easy way to fix this is to make tap a lazy var:

lazy var tap: UITapGestureRecognizer = {
    let t = UITapGestureRecognizer(target: self, action: #selector(tapped))
    return t
}()

然后,您第一次访问 tap 时,将创建点击手势识别器,届时将创建您的自定义单元格,self 将引用类的实例.

Then, the first time you access tap, the tap gesture recognizer will be created and at that time, your custom cell will be created and self will refer to the instance of the class.

或者,您可以使 tap 成为计算属性:

Alternatively, you can make tap a computed property:

var tap: UITapGestureRecognizer {
    let t = UITapGestureRecognizer(target: self, action: #selector(tapped))
    return t
}

tap 会在被访问时创建并返回一个 UITapGestureRecognizer.同样,在这种情况下,将创建自定义单元格,因此 self 将正确引用类的实例.

and tap will create and return a UITapGestureRecognizer when it is accessed. Again, in that case, the custom cell will be created, so self will properly refer to the instance of the class.

这篇关于添加到 collectionview 单元格中的 uiview 时,点击手势未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆