UITapGestureRecognizer working on UIImageView but not on UILabel

后端 未结 2 1003
渐次进展
渐次进展 2021-01-24 07:25

I have a UITableViewCell class named CommentsTableViewCell which among other things includes a UIImageView and a UILabel.

相关标签:
2条回答
  • 2021-01-24 07:45

    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.

    0 讨论(0)
  • 2021-01-24 07:48

    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)
    
    0 讨论(0)
提交回复
热议问题