Chatto
Chatto copied to clipboard
Custom long press pop menu
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
}
}
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
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?
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.
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 }
I almost tried every thing to add new UIMenuItem ... but without success ... please some try to help us ...
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?