Skip to content

Commit

Permalink
Fix message view tap gesture (original PR: MessageKit#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
lufzi committed Dec 20, 2017
1 parent 1231def commit babec66
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Example/Sources/ConversationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ extension ConversationViewController: MessageCellDelegate {
print("Bottom label tapped")
}

func didTapCellContent(in cell: MessageCollectionViewCell) {
print("Cell content tapped")
}
}

// MARK: - MessageLabelDelegate
Expand Down
7 changes: 7 additions & 0 deletions Sources/Protocols/MessageCellDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public protocol MessageCellDelegate: MessageLabelDelegate {
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapTopLabel(in cell: MessageCollectionViewCell)

/// Trigerred when a touch occures in cell content.
///
/// - Parameter cell: The cell where the touch occurred.
/// `indexPath(for: cell)` method. Then using the returned `IndexPath` with the `MessagesDataSource`
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapCellContent(in cell: MessageCollectionViewCell)
}

public extension MessageCellDelegate {
Expand All @@ -79,4 +85,5 @@ public extension MessageCellDelegate {

func didTapTopLabel(in cell: MessageCollectionViewCell) {}

func didTapCellContent(in cell: MessageCollectionViewCell) {}
}
11 changes: 2 additions & 9 deletions Sources/Views/Cells/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab
super.init(frame: frame)
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
setupSubviews()
setupGestureRecognizers()
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -115,18 +114,12 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab
cellBottomLabel.attributedText = bottomText
}

func setupGestureRecognizers() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
contentView.addGestureRecognizer(tapGesture)
}

/// Handle tap gesture on contentView and its subviews like messageContainerView, cellTopLabel, cellBottomLabel, avatarView ....
@objc
open func handleTapGesture(_ gesture: UIGestureRecognizer) {
guard gesture.state == .ended else { return }

let touchLocation = gesture.location(in: self)

delegate?.didTapCellContent(in: self)

switch true {
case messageContainerView.frame.contains(touchLocation) && !cellContentView(canHandle: convert(touchLocation, to: messageContainerView)):
delegate?.didTapMessage(in: self)
Expand Down
22 changes: 20 additions & 2 deletions Sources/Views/MessagesCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ open class MessagesCollectionView: UICollectionView {
public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
backgroundColor = .white
setupGestureRecognizers()
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -63,6 +64,23 @@ open class MessagesCollectionView: UICollectionView {

// MARK: - Methods

func setupGestureRecognizers() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
tapGesture.delaysTouchesBegan = true
addGestureRecognizer(tapGesture)
}

@objc
open func handleTapGesture(_ gesture: UIGestureRecognizer) {
guard gesture.state == .ended else { return }

let touchLocation = gesture.location(in: self)
guard let indexPath = indexPathForItem(at: touchLocation) else { return }

let cell = cellForItem(at: indexPath) as? MessageCollectionViewCell
cell?.handleTapGesture(gesture)
}

public func scrollToBottom(animated: Bool = false) {
let collectionViewContentHeight = collectionViewLayout.collectionViewContentSize.height

Expand All @@ -74,13 +92,13 @@ open class MessagesCollectionView: UICollectionView {
public func reloadDataAndKeepOffset() {
// stop scrolling
setContentOffset(contentOffset, animated: false)

// calculate the offset and reloadData
let beforeContentSize = contentSize
reloadData()
layoutIfNeeded()
let afterContentSize = contentSize

// reset the contentOffset after data is updated
let newOffset = CGPoint(
x: contentOffset.x + (afterContentSize.width - beforeContentSize.width),
Expand Down

0 comments on commit babec66

Please sign in to comment.