我试图在MessageCollectionView
点击时放大单元格中的图像,这是大多数消息传递应用程序中的标准。我成功地能够识别用户对每个单元格的点击并确定它是照片还是文本单元格,但我的imageTapped
功能似乎没有触发。img
我在imageTapped
函数内部传递给函数的值didTapMessage
看起来像UIImageView
预期的那样。里面的控制台打印imageTapped
似乎没有触发,所以它永远不会进入那个函数。也许我在这里遗漏了一些简单的东西,但我觉得它应该通过那个功能!
我正在使用 Swift 5、iOS 13.2 和 MessageKit。
代码:
// MARK: - MessageCellDelegate
// With help from:
// - https://github.com/MessageKit/MessageKit/issues/778
// - https://stackoverflow.com/questions/45536405/full-screen-an-image-inside-a-tableviewcell-when-tapped-swift
// - https://github.com/MessageKit/MessageKit/issues/261
extension RoomVC: MessageCellDelegate {
func didTapMessage(in cell: MessageCollectionViewCell) {
guard let indexPath = messagesCollectionView.indexPath(for: cell) else { return }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return }
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
switch message.kind {
case .photo(let photoItem):
log.info("Message is a photo.")
if let img = photoItem.image{
self.imageTapped(image: img)
}
default:
log.info("Message is not a photo.")
break
}
}
func imageTapped(image: UIImage){
log.info("Entered the imageTapped function.")
let newImageView = UIImageView(image: image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
}
@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
}