MessageKit animation after click on message

696 Views Asked by At

I tried added in my app the same animation which, we can find in messenger, after click on some message, app shows date label. I can showed this date, but i can't add smooth animation. I tried added uiview.animate(withDuration), but in another topic I readed, that I need called method layoutIfNeed, but i can't handle any object from messagekit. Somebody can help me, or maybe gives me another way to do this sliding animation

func didTapMessage(in cell: MessageCollectionViewCell) {
    if isClicked {
        isClicked = false
        if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
            layout.setMessageOutgoingMessagePadding(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    } else {
        isClicked = true
        if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
            UIView.animate(withDuration: 3) {
                //MARK: how called layoufIfNeeded()? or how do this animation another way
                layout.collectionView?.layoutIfNeeded()
                layout.setMessageOutgoingMessagePadding(UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0))
            }
        }
    }
    let indexPath = self.messagesCollectionView.indexPath(for: cell)!
    messagesCollectionView.reloadItems(at: [indexPath])
}

before Clickafter Click

1

There are 1 best solutions below

1
On

First: You should create an array [IndexPath] to save indexPath of messages you tapped in

func didTapMessage(in cell: MessageCollectionViewCell) 

Second: You should use some function of MessagesLayoutDelegate protocol and MessagesDataSource protocol to set height or content for top, bottom message such as Date time, status message, ...

 func cellTopLabelHeight(for message: MessageKit.MessageType, at indexPath: IndexPath, in messagesCollectionView:
    MessageKit.MessagesCollectionView) -> CGFloat
        func messageTopLabelHeight(for message: MessageKit.MessageType, at indexPath: IndexPath, in messagesCollectionView:
    MessageKit.MessagesCollectionView) -> CGFloat

func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?
 func cellBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?

In there function, will check indexPath equal your saved indexPath tapped to show or hide Date time. It's up to your logical.