From a132694301bfc5103852436e1ba3d65941fb3093 Mon Sep 17 00:00:00 2001 From: Damian Kanak Date: Fri, 26 May 2017 15:46:24 +0200 Subject: [PATCH 1/2] Slide in animation for newly created message bubble --- Demo/Base.lproj/Main.storyboard | 6 ++--- Demo/ViewController.swift | 2 +- MessagesView/MessageCollectionViewCell.swift | 28 +++++++++++++++++--- MessagesView/MessageCollectionViewCell.xib | 9 ++++--- MessagesView/MessagesView.swift | 19 ++++++++++++- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Demo/Base.lproj/Main.storyboard b/Demo/Base.lproj/Main.storyboard index 8bc60ab..7127272 100644 --- a/Demo/Base.lproj/Main.storyboard +++ b/Demo/Base.lproj/Main.storyboard @@ -1,10 +1,10 @@ - - + + - + diff --git a/Demo/ViewController.swift b/Demo/ViewController.swift index f78d006..ca00b19 100644 --- a/Demo/ViewController.swift +++ b/Demo/ViewController.swift @@ -44,7 +44,7 @@ extension ViewController: MessagesViewDelegate { func didTapRightButton() { let text = messagesView.inputText TestData.exampleMessageText.append(text) - messagesView.refresh(scrollToLastMessage: true) + messagesView.refresh(scrollToLastMessage: true, animateLastMessage: true) } } diff --git a/MessagesView/MessageCollectionViewCell.swift b/MessagesView/MessageCollectionViewCell.swift index 8937eb5..81529da 100644 --- a/MessagesView/MessageCollectionViewCell.swift +++ b/MessagesView/MessageCollectionViewCell.swift @@ -39,10 +39,10 @@ class MessageCollectionViewCell: UICollectionViewCell { @IBOutlet weak var labelTopConstraint: NSLayoutConstraint! @IBOutlet weak var labelBottomConstraint: NSLayoutConstraint! + @IBOutlet weak var backgroundTopConstraint: NSLayoutConstraint! @IBOutlet weak var backgroundTrailingConstraint: NSLayoutConstraint! @IBOutlet weak var backgroundLeadingConstraint: NSLayoutConstraint! - - @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! + @IBOutlet weak var backgroundBottomConstraint: NSLayoutConstraint! private let defaultBubbleMargin: CGFloat = 8 private let additionalTextLabelVerticalSpacing: CGFloat = 4 @@ -135,7 +135,7 @@ class MessageCollectionViewCell: UICollectionViewCell { labelBottomConstraint.constant = textInsets.bottom } - bottomSpacingConstraint.constant = bottomSpacing + backgroundBottomConstraint.constant = bottomSpacing } func size(message: String, width: CGFloat, bubbleImage: BubbleImage, minimalHorizontalSpacing: CGFloat, @@ -186,4 +186,26 @@ class MessageCollectionViewCell: UICollectionViewCell { minimalHorizontalSpacing = settings.minimalHorizontalSpacing } + + func slideIn() { + let horizontalConstraint: NSLayoutConstraint! + switch self.side { + case .left: + horizontalConstraint = backgroundTrailingConstraint + case .right: + horizontalConstraint = backgroundLeadingConstraint + } + + let horizontal = horizontalConstraint.constant + let top = backgroundTopConstraint.constant + backgroundTopConstraint.constant = self.textLabel.frame.height + horizontalConstraint.constant = contentView.frame.size.width * 2 + layoutIfNeeded() + + UIView.animate(withDuration: 0.25) { + horizontalConstraint.constant = horizontal + self.backgroundTopConstraint.constant = top + self.contentView.layoutIfNeeded() + } + } } diff --git a/MessagesView/MessageCollectionViewCell.xib b/MessagesView/MessageCollectionViewCell.xib index dfb5ae1..a61a220 100644 --- a/MessagesView/MessageCollectionViewCell.xib +++ b/MessagesView/MessageCollectionViewCell.xib @@ -1,10 +1,10 @@ - - + + - + @@ -60,9 +60,10 @@ + + - diff --git a/MessagesView/MessagesView.swift b/MessagesView/MessagesView.swift index 66d0c51..0425493 100644 --- a/MessagesView/MessagesView.swift +++ b/MessagesView/MessagesView.swift @@ -89,6 +89,8 @@ public class MessagesView: UIView { public var delegate : MessagesViewDelegate? public var dataSource: MessagesViewDataSource? + public var isLastMessageAnimated = false + //MARK:- var bubbleImageLeft: BubbleImage = BubbleImage(cornerRadius: 8) @@ -145,8 +147,9 @@ public class MessagesView: UIView { apply(settings: settings) } - public func refresh(scrollToLastMessage: Bool) { + public func refresh(scrollToLastMessage: Bool, animateLastMessage: Bool = false) { DispatchQueue.main.async { + self.isLastMessageAnimated = animateLastMessage self.messagesCollectionView.reloadData() if scrollToLastMessage { @@ -447,4 +450,18 @@ extension MessagesView: UICollectionViewDelegateFlowLayout { return size } + + func isLastMessage(indexPath: IndexPath)->Bool { + let lastMessageIndex = (dataSource?.messages.count ?? 0) - 1 + return indexPath.row == lastMessageIndex + } + + public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + guard let cell = cell as? MessageCollectionViewCell else { + return + } + if isLastMessage(indexPath: indexPath) && isLastMessageAnimated { + cell.slideIn() + } + } } From bce5527588ee61f681f36c2182b601b4e290febd Mon Sep 17 00:00:00 2001 From: Damian Kanak Date: Fri, 26 May 2017 18:41:24 +0200 Subject: [PATCH 2/2] hiding keyboard when tapped collectionView area --- MessagesView/MessagesInputToolbar.swift | 4 ++++ MessagesView/MessagesToolbarContentView.swift | 8 ++++++-- MessagesView/MessagesView.swift | 3 +++ MessagesView/MessagesView.xib | 14 +++++++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/MessagesView/MessagesInputToolbar.swift b/MessagesView/MessagesInputToolbar.swift index 99aa511..1f5bfe3 100644 --- a/MessagesView/MessagesInputToolbar.swift +++ b/MessagesView/MessagesInputToolbar.swift @@ -35,6 +35,10 @@ class MessagesInputToolbar: UIToolbar { toolbarContentView.frame = self.bounds } + override func resignFirstResponder() -> Bool { + return toolbarContentView.resignFirstResponder() + } + func rightButton(show: Bool, animated: Bool) { toolbarContentView.righButton(show: show, animated: animated) } diff --git a/MessagesView/MessagesToolbarContentView.swift b/MessagesView/MessagesToolbarContentView.swift index e25c8ba..78c1607 100644 --- a/MessagesView/MessagesToolbarContentView.swift +++ b/MessagesView/MessagesToolbarContentView.swift @@ -61,7 +61,7 @@ class MessagesToolbarContentView: UIView { } if settings.leftButtonHidesKeyboard { - messageEditorTextView.resignFirstResponder() + _ = resignFirstResponder() } } @@ -72,10 +72,14 @@ class MessagesToolbarContentView: UIView { } if settings.rightButtonHidesKeyboard { - messageEditorTextView.resignFirstResponder() + _ = resignFirstResponder() } } + override func resignFirstResponder() -> Bool { + return messageEditorTextView.resignFirstResponder() + } + class func fromNib() -> MessagesToolbarContentView { let bundle = Bundle(for: MessagesToolbarContentView.classForCoder()) let nibViews = bundle.loadNibNamed(String(describing: MessagesToolbarContentView.self), owner: nil, options: nil) diff --git a/MessagesView/MessagesView.swift b/MessagesView/MessagesView.swift index 0425493..9bc0822 100644 --- a/MessagesView/MessagesView.swift +++ b/MessagesView/MessagesView.swift @@ -273,6 +273,9 @@ public class MessagesView: UIView { messagesCollectionView.register(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: Key.messagesCollectionViewFooter) } + @IBAction func didTapCollectionViewArea(_ sender: Any) { + _ = messagesInputToolbar.resignFirstResponder() + } public func refresh() { DispatchQueue.main.async { diff --git a/MessagesView/MessagesView.xib b/MessagesView/MessagesView.xib index a125e37..7222bda 100644 --- a/MessagesView/MessagesView.xib +++ b/MessagesView/MessagesView.xib @@ -1,10 +1,10 @@ - - + + - + @@ -23,6 +23,7 @@ + @@ -33,6 +34,7 @@ + @@ -46,6 +48,7 @@ + @@ -56,5 +59,10 @@ + + + + +