From 009bc18bde40275fcce4eca707633957c294d439 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 23 Jul 2015 16:01:07 +0800 Subject: [PATCH] longPressAction for ChatLeftTextCell show menu --- .../ConversationViewController.swift | 52 +++++++++++++++++++ .../Cells/ChatLeftText/ChatLeftTextCell.swift | 20 ++++--- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index b2cf7589..8e625a14 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -1935,6 +1935,58 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi cell.configureWithMessage(message, textContentLabelWidth: textContentLabelWidthOfMessage(message), collectionView: collectionView, indexPath: indexPath) + cell.longPressAction = { [weak self] cell in + + self?.currentMenu?.hide() + + if let strongSelf = self { + + let copyItem = BubbleMenuView.Item(title: NSLocalizedString("Copy", comment: "")) { menu in + print("copy\n") + + UIPasteboard.generalPasteboard().string = cell.textContentTextView.text + + menu.hide() + } + + let textViewFrame = cell.convertRect(cell.textContentTextView.frame, toView: strongSelf.view) + + let arrowDirection: BubbleMenuView.ArrowDirection = CGRectGetMidY(textViewFrame) < 64 + 80 ? .Up : .Down + + let menu = BubbleMenuView(arrowDirection: arrowDirection, items: [copyItem]) + + strongSelf.currentMenu = menu + + menu.setTranslatesAutoresizingMaskIntoConstraints(false) + + strongSelf.view.addSubview(menu) + + + var vConstant = CGRectGetMidY(textViewFrame) - CGRectGetMidY(strongSelf.conversationCollectionView.frame) + + let menuV: NSLayoutConstraint + + switch arrowDirection { + + case .Up: + vConstant += ceil(CGRectGetHeight(textViewFrame) * 0.5) - menu.offsetV + + menuV = NSLayoutConstraint(item: menu, attribute: .Top, relatedBy: .Equal, toItem: strongSelf.view, attribute: .CenterY, multiplier: 1, constant: vConstant) + + case .Down: + vConstant -= ceil(CGRectGetHeight(textViewFrame) * 0.5) - menu.offsetV + + menuV = NSLayoutConstraint(item: menu, attribute: .Bottom, relatedBy: .Equal, toItem: strongSelf.view, attribute: .CenterY, multiplier: 1, constant: vConstant) + } + + let centerXConstant = CGRectGetMidX(textViewFrame) - CGRectGetMidX(strongSelf.conversationCollectionView.frame) + + let menuCenterX = NSLayoutConstraint(item: menu, attribute: .CenterX, relatedBy: .Equal, toItem: strongSelf.view, attribute: .CenterX, multiplier: 1, constant: centerXConstant) + + NSLayoutConstraint.activateConstraints([menuV, menuCenterX]) + } + } + return cell } diff --git a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift index de7f7919..66467118 100644 --- a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift +++ b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift @@ -22,6 +22,8 @@ class ChatLeftTextCell: UICollectionViewCell { @IBOutlet weak var textContentTextViewLeadingConstraint: NSLayoutConstraint! @IBOutlet weak var textContentTextViewWidthConstraint: NSLayoutConstraint! + var longPressAction: (ChatLeftTextCell -> Void)? + override func awakeFromNib() { super.awakeFromNib() @@ -58,15 +60,17 @@ class ChatLeftTextCell: UICollectionViewCell { } func handleLongPress(longPress: UILongPressGestureRecognizer) { - if longPress.state == .Began { - if let view = longPress.view, superview = view.superview { - view.becomeFirstResponder() +// if longPress.state == .Began { +// if let view = longPress.view, superview = view.superview { +// view.becomeFirstResponder() +// +// let menu = UIMenuController.sharedMenuController() +// menu.setTargetRect(view.frame, inView: superview) +// menu.setMenuVisible(true, animated: true) +// } +// } - let menu = UIMenuController.sharedMenuController() - menu.setTargetRect(view.frame, inView: superview) - menu.setMenuVisible(true, animated: true) - } - } + longPressAction?(self) } func configureWithMessage(message: Message, textContentLabelWidth: CGFloat, collectionView: UICollectionView, indexPath: NSIndexPath) {