[英]Doubletap recognizer for message bubbles (Messagekit)
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".我收到此错误线程 1:“-[MessageKit.MessagesCollectionView doubleTapWithGesture:]:无法识别的选择器已发送到实例 0x7f94c7112a00”。
If you think my post isn't clear in anyway, please let me know so I can clarify it.如果您认为我的帖子无论如何都不清楚,请告诉我,以便我澄清。 Thank you谢谢
You are adding UITapGestureRecognizer
on view from MessagesViewController
, which will attach it to the same controller view for each cell.您正在从MessagesViewController
的视图上添加UITapGestureRecognizer
,这会将其附加到每个单元格的相同控制器视图。
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.我想我们正在尝试双击message cell
,为了实现这一点,请在您尝试双击的单元格中添加UITapGestureRecognizer
。
Suppose, we have DoubleTappableCell
, which can be a text cell, media cell, or any other message.假设我们有DoubleTappableCell
,它可以是文本单元格、媒体单元格或任何其他消息。
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
.此单元格将从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.另一种替代但改进的方法是使用ViewController
中的一个UITapGestureRecognizer
来检测被触摸单元格的索引路径,从该索引中获取相关单元格,并基于该索引执行操作和查看更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.