UITapGestureRecognizer working on UIImageView but not on UILabel

风格不统一 提交于 2019-12-02 02:59:28
KKRocks

You need different gesture for all control

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
avatarRoundImageView.userInteractionEnabled = true
avatarRoundImageView.addGestureRecognizer(tapGesture)

let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
nameLabel.userInteractionEnabled = true
nameLabel.addGestureRecognizer(tapGesture2)

You need to create two UITapGestureRecognizer object because UITapGestureRecognizer works with single UI element object. So create second TapGestureRecognizer and assign one to UILabel and one to UIImageView.

From UIGestureRecognizer documentation.

A gesture recognizer operates on touches hit-tested to a specific view and all of that view’s subviews. It thus must be associated with that view. To make that association you must call the UIView method addGestureRecognizer(_:). A gesture recognizer doesn’t participate in the view’s responder chain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!