forked from badoo/Chatto
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |