1

I want to add the feature to double tap a given message bubble.

Code so far:

@objc func doubleTap(gesture: UITapGestureRecognizer) {
    print("double tap called")
}

func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
    
    let tapGesture = UITapGestureRecognizer(target: messagesCollectionView, action: #selector(doubleTap(gesture:)))
        tapGesture.numberOfTapsRequired = 2
    view.addGestureRecognizer(tapGesture)
    
    let sender = message.sender
    if sender.senderId == selfSenderEmail?.senderId {
        // Self Message
        return .bubbleTailOutline(.blue, .bottomRight, .curved)
    }
    // More if statements for groupchat color coding
    return .bubbleTailOutline(.darkGray, .bottomLeft, .curved)
}

I get this Error Thread 1: "-[MessageKit.MessagesCollectionView doubleTapWithGesture:]: unrecognized selector sent to instance 0x7f94c7112a00".

If you think my post isn't clear in anyway, please let me know so I can clarify it. Thank you

1 Answers1

0

You are adding UITapGestureRecognizer on view from MessagesViewController, which will attach it to the same controller view for each cell.

I suppose we are trying to get a double-tap for message cell and in order to achieve that, add UITapGestureRecognizer in the cell that you are trying to get the double tap on.

Suppose, we have DoubleTappableCell, which can be a text cell, media cell, or any other message.

class DoubleTappableCell: MessageContentCell {
    
    lazy var doubleTapRecognizer: UITapGestureRecognizer = {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
        tapGesture.numberOfTapsRequired = 2
        return tapGesture
    }()
    
    var onDoubleTapped: () -> Void = {}
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addGestureRecognizer(doubleTapRecognizer)
    }
    
    @objc func doubleTapped() {
        onDoubleTapped()
    }
}

This cell will be returned from the MessageCollectionView.


public override func collectionView(_ collectionView: UICollectionView,
                                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    guard let cell: VideoCell = collectionView
        .dequeueReusableCell(indexPath: index) else { return UICollectionViewCell() }

    cell.data = anyData

    cell.onDoubleTapped = {
        print("DoubleTapped")
    }
}

Another alternate but improved approach would be to use one UITapGestureRecognizer from ViewController to detect the indexpath of the touched cell, get the relevant cell from that index, and execute actions and view updates based on that.

tanmoy
  • 1,276
  • 1
  • 10
  • 28