diff --git a/ChattoApp/ChattoApp.xcodeproj/project.pbxproj b/ChattoApp/ChattoApp.xcodeproj/project.pbxproj index ad3b56e7a..c450ea851 100644 --- a/ChattoApp/ChattoApp.xcodeproj/project.pbxproj +++ b/ChattoApp/ChattoApp.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ C3F91DCC1C75EFE300D461D2 /* SendingStatusCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3F91DC91C75EFE300D461D2 /* SendingStatusCollectionViewCell.swift */; }; C3F91DCD1C75EFE300D461D2 /* SendingStatusCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = C3F91DCA1C75EFE300D461D2 /* SendingStatusCollectionViewCell.xib */; }; C3F91DCE1C75EFE300D461D2 /* SendingStatusPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3F91DCB1C75EFE300D461D2 /* SendingStatusPresenter.swift */; }; + FE0A3F2C1CC80D1D00560152 /* PKeyboardHider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE0A3F2B1CC80D1D00560152 /* PKeyboardHider.swift */; }; FE2D050B1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */; }; /* End PBXBuildFile section */ @@ -103,6 +104,7 @@ C3F91DC91C75EFE300D461D2 /* SendingStatusCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SendingStatusCollectionViewCell.swift; sourceTree = ""; }; C3F91DCA1C75EFE300D461D2 /* SendingStatusCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SendingStatusCollectionViewCell.xib; sourceTree = ""; }; C3F91DCB1C75EFE300D461D2 /* SendingStatusPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SendingStatusPresenter.swift; sourceTree = ""; }; + FE0A3F2B1CC80D1D00560152 /* PKeyboardHider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKeyboardHider.swift; sourceTree = ""; }; FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseMessageCollectionViewCellAvatarStyle.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -228,6 +230,7 @@ C3F91DB11C75EF9E00D461D2 /* Text Messages */, C341D42A1C96359000FD3463 /* Time Separator */, FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */, + FE0A3F2B1CC80D1D00560152 /* PKeyboardHider.swift */, ); path = Source; sourceTree = ""; @@ -468,6 +471,7 @@ C3F91DC71C75EF9E00D461D2 /* BaseMessageHandler.swift in Sources */, C3F91DC61C75EF9E00D461D2 /* DemoTextMessageViewModel.swift in Sources */, C341D42E1C9635DF00FD3463 /* TimeSeparatorModel.swift in Sources */, + FE0A3F2C1CC80D1D00560152 /* PKeyboardHider.swift in Sources */, C3F91DC01C75EF9E00D461D2 /* DemoPhotoMessageHandler.swift in Sources */, C3F91DBC1C75EF9E00D461D2 /* DemoChatViewController.swift in Sources */, C3F91DBD1C75EF9E00D461D2 /* FakeDataSource.swift in Sources */, diff --git a/ChattoApp/ChattoApp/Source/DemoChatViewController.swift b/ChattoApp/ChattoApp/Source/DemoChatViewController.swift index b1bf271d6..b422995f2 100644 --- a/ChattoApp/ChattoApp/Source/DemoChatViewController.swift +++ b/ChattoApp/ChattoApp/Source/DemoChatViewController.swift @@ -36,6 +36,9 @@ class DemoChatViewController: BaseChatViewController { } } + private var keyboardHider: PKeyboardHider! + private var chatInputView: ChatInputBar! + lazy private var baseMessageHandler: BaseMessageHandler = { return BaseMessageHandler(messageSender: self.messageSender) }() @@ -46,6 +49,7 @@ class DemoChatViewController: BaseChatViewController { super.chatItemsDecorator = ChatItemsDemoDecorator() let addIncomingMessageButton = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(DemoChatViewController.addRandomIncomingMessage)) self.navigationItem.rightBarButtonItem = addIncomingMessageButton + self.keyboardHider = PKeyboardHider(collectionView: self.collectionView, inputBar: self.chatInputView, hideCollectionView: false) } @objc @@ -55,7 +59,7 @@ class DemoChatViewController: BaseChatViewController { var chatInputPresenter: BasicChatInputBarPresenter! override func createChatInputView() -> UIView { - let chatInputView = ChatInputBar.loadNib() + chatInputView = ChatInputBar.loadNib() var appearance = ChatInputBarAppearance() appearance.sendButtonTitle = NSLocalizedString("Send", comment: "") appearance.textPlaceholder = NSLocalizedString("Type a message", comment: "") diff --git a/ChattoApp/ChattoApp/Source/PKeyboardHider.swift b/ChattoApp/ChattoApp/Source/PKeyboardHider.swift new file mode 100644 index 000000000..f836d610b --- /dev/null +++ b/ChattoApp/ChattoApp/Source/PKeyboardHider.swift @@ -0,0 +1,69 @@ +// +// PKeyboardHidder.swift +// Plato +// +// Created by Zhao Wang on 12/20/15. +// Copyright © 2015 Plato Team Inc. All rights reserved. +// + +import Foundation +import UIKit +import ChattoAdditions + +class PKeyboardHider: NSObject, UIGestureRecognizerDelegate { + + + private let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer() + private let collectionView: UICollectionView + private let chatInputBar: ChatInputBar + private var hideCollectionView: Bool + + init(collectionView: UICollectionView, inputBar: ChatInputBar, hideCollectionView: Bool = false) { + self.collectionView = collectionView + self.chatInputBar = inputBar + self.hideCollectionView = hideCollectionView + super.init() + self.collectionView.addGestureRecognizer(self.tapRecognizer) + self.tapRecognizer.addTarget(self, action: #selector(PKeyboardHider.handleTap(_:))) + self.tapRecognizer.delegate = self + } + + @objc + private func handleTap(tapRecognizer: UITapGestureRecognizer) { + switch tapRecognizer.state { + case .Ended: + self.chatInputBar.endEditing(true) + break + default: + break; + } + } + + func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return true + } + + func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool { + if self.collectionView.hidden && self.hideCollectionView { + return false + } + + if gestureRecognizer != self.tapRecognizer { + return true + } + + let point = gestureRecognizer.locationInView(self.collectionView) + for cell in self.collectionView.visibleCells() { + // Need to check if the touch is outside of the bubble view, if so, treat as in game view area + if cell is BaseMessageCollectionViewCell { + let msgCell = cell as! BaseMessageCollectionViewCell + let locationInView = msgCell.bubbleView.convertPoint(point, fromView: self.collectionView) + if CGRectContainsPoint(msgCell.bubbleView.bounds, locationInView) { + return false + } + } + } + + return true + } +}