I am trying to enlarge an image in the MessageCollectionView
in a cell when it is tapped, something standard in most messaging apps. I successfully am able to recognize the user's tap on each cell and determine if it is a photo or a text cell, but my imageTapped
function doesnt seem to be triggering. The img
value I am passing to the imageTapped
function inside the didTapMessage
function and looks like it is a UIImageView
as expected. The console print inside imageTapped
doesn't seem to be triggering, so it never enters that function. Maybe I am missing something simple here, but I feel like it should be going thru that function!
I am using Swift 5, iOS 13.2, and MessageKit.
Code:
// 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()
}
}