チャットの長押しをして、通報というのを新たに追加したかったのだが、例によってMessageKitのマニュアルにはのってなかったのでやり方のメモ。
基本的には、MessageKitはUICollectionViewを継承してるので、UICollectionViewの長押しの設定と同じようなことをやればよい。
cf: Delete message by long press on cell ? #706
class ViewController: MessagesViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 新しくロングタップメニューを追加する
let reportMenuItem = UIMenuItem(title: "通報", action: #selector(MessageCollectionViewCell.report(_:)))
UIMenuController.shared.menuItems = [reportMenuItem]
}
// ロングタップで表示するメニューの設定
override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
// actionは結構色んな種類(cooy, deleteなど)がデフォルトで定義されているので必要であればtrueにすればメニューに表示されるようになる
switch action {
case NSSelectorFromString("report:"):
return true
default:
return super.collectionView(collectionView, canPerformAction: action, forItemAt: indexPath, withSender: sender)
}
}
// メニューをタップして発火されるイベントの設定
override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
switch action {
case NSSelectorFromString("report:"):
// ここで通報処理する
default:
super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
}
}
}
extension MessageCollectionViewCell {
// イベント発火させるためのメソッド
@objc func report(_ sender: Any?) {
// Get the collectionView
if let collectionView = self.superview as? UICollectionView {
// Get indexPath
if let indexPath = collectionView.indexPath(for: self) {
// Trigger action
collectionView.delegate?.collectionView?(collectionView, performAction: NSSelectorFromString("report:"), forItemAt: indexPath, withSender: sender)
}
}
}
} MessageKitはいろいろできるけどいろいろと自分で工夫しないと行けない点がたくさんです。
こんにちは。virapture…
View Comments
I don't think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/ru/register?ref=B4EPR6J0
I don't think the title of your enticle matches the content lol. Just kidding, mainly because I had some doubts after reading the enticle. https://www.binance.com/en/register?ref=P9L9FQKY
I am currently writing a paper that is very related to your content. I read your article and I have some questions. I would like to ask you. Can you answer me? I'll keep an eye out for your reply. 20bet
Thank you very much for sharing, I learned a lot from your article. Very cool. Thanks. nimabi
Your article helped me a lot, is there any more related content? Thanks! https://www.binance.com/lv/register?ref=OMM3XK51
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.info/id/join?ref=FIHEGIZ8
Your article helped me a lot, is there any more related content? Thanks!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your article helped me a lot, is there any more related content? Thanks!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.