Chatto icon indicating copy to clipboard operation
Chatto copied to clipboard

Custom long press pop menu

Open jkdntc opened this issue 8 years ago • 6 comments

How to custom long press pop menu?

This is my code:

let menuItem: UIMenuItem = UIMenuItem(title: "speech", action: #selector(AudioMessageCollectionViewCell.speech(_:))) UIMenuController.sharedMenuController().menuItems = [menuItem] UIMenuController.sharedMenuController().update()

public class AudioMessageCollectionViewCell: BaseMessageCollectionViewCell<AudioBubbleView> {

override public func canBecomeFirstResponder() -> Bool {
    return true
}

public override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    if action == #selector(AudioMessageCollectionViewCell.speech(_:)) {
        return true
    } else {
        return false
    }
}

func speech(sender: AnyObject) {
    // your custom action
}

}

jkdntc avatar May 31 '16 02:05 jkdntc

You also need:

public protocol ChatItemPresenterProtocol: class {
    ...
    func shouldShowMenu() -> Bool // optional. Default is false
    func canPerformMenuControllerAction(action: Selector) -> Bool // optional. Default is false
    func performMenuControllerAction(action: Selector) // optional

diegosanchezr avatar May 31 '16 16:05 diegosanchezr

func performMenuControllerAction(action: Selector) //You can handle standard actions here, but custom actions never trigger this.Is there an easier way to handle custom menu actions?

jkdntc avatar Jun 01 '16 04:06 jkdntc

We're not doing anything special here. Whatever works for a standard UICollectionView should work here in the same way. The framework just redirects the delegate methods to the presenters.

diegosanchezr avatar Jun 01 '16 13:06 diegosanchezr

Temporary solution:

BaseMessageCollectionViewCell.swift: func popMenuPerformAction(selector:String) { var v = self.superview while v != nil { if let collectionView = v as? UICollectionView { if let indexPath = collectionView.indexPathForCell(self) { collectionView.delegate?.collectionView!(collectionView, performAction: Selector(selector), forItemAtIndexPath: indexPath, withSender: "") break } } else { v = v?.superview } } }

public func popMenu1(sender: AnyObject) {
    popMenuPerformAction("popMenu1")
}

public func popMenu2(sender: AnyObject) {
    popMenuPerformAction("popMenu2")
}

public func popMenu3(sender: AnyObject) {
    popMenuPerformAction("popMenu3")
}

public func popMenu4(sender: AnyObject) {
    popMenuPerformAction("popMenu4")
}

class ChattoViewController: BaseChatViewController { .... UIMenuController.sharedMenuController().menuItems = menuItems .... override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) { } ... }

DemoTextMessagePresenter.swift: public override func canPerformMenuControllerAction(action: Selector) -> Bool { for menuItem in UIMenuController.sharedMenuController().menuItems! { if action == menuItem.action { return true } } return false }

jkdntc avatar Jun 17 '16 10:06 jkdntc

I almost tried every thing to add new UIMenuItem ... but without success ... please some try to help us ...

albarq avatar Jun 05 '18 17:06 albarq

Has anyone answered this? This is ridiculous that the makers of Chatto have not made a clear example of how to add a popup context menu for long press. Most chat applications have the opportunity to delete an item... why is this not shown as a standard example?

diadempro avatar Apr 07 '19 05:04 diadempro