Skip to content

Commit

Permalink
Keyboard jumps on iOS 8
Browse files Browse the repository at this point in the history
  • Loading branch information
zwang committed Apr 20, 2016
1 parent 8536416 commit c2bd52b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChattoApp/ChattoApp.xcodeproj/project.pbxproj
Expand Up @@ -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 */

Expand Down Expand Up @@ -103,6 +104,7 @@
C3F91DC91C75EFE300D461D2 /* SendingStatusCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SendingStatusCollectionViewCell.swift; sourceTree = "<group>"; };
C3F91DCA1C75EFE300D461D2 /* SendingStatusCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SendingStatusCollectionViewCell.xib; sourceTree = "<group>"; };
C3F91DCB1C75EFE300D461D2 /* SendingStatusPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SendingStatusPresenter.swift; sourceTree = "<group>"; };
FE0A3F2B1CC80D1D00560152 /* PKeyboardHider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKeyboardHider.swift; sourceTree = "<group>"; };
FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseMessageCollectionViewCellAvatarStyle.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -228,6 +230,7 @@
C3F91DB11C75EF9E00D461D2 /* Text Messages */,
C341D42A1C96359000FD3463 /* Time Separator */,
FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */,
FE0A3F2B1CC80D1D00560152 /* PKeyboardHider.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
6 changes: 5 additions & 1 deletion ChattoApp/ChattoApp/Source/DemoChatViewController.swift
Expand Up @@ -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)
}()
Expand All @@ -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
Expand All @@ -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: "")
Expand Down
69 changes: 69 additions & 0 deletions 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<TextBubbleView> {
let msgCell = cell as! BaseMessageCollectionViewCell<TextBubbleView>
let locationInView = msgCell.bubbleView.convertPoint(point, fromView: self.collectionView)
if CGRectContainsPoint(msgCell.bubbleView.bounds, locationInView) {
return false
}
}
}

return true
}
}

0 comments on commit c2bd52b

Please sign in to comment.