Because I like watching jokes, I recently made an APP to watch jokes. Mainly because of the way Netease News and Toutiao shared their jokes.

The finished effect





2017-02-21 16_00_55.gif

  • You just click on the piece you want to share
  • Then choose QQ or Wechat and click paste after the jump. You don’t need to share a link like netease News and Toutiao. Others have to click in if they want to see it. Of course they can replicate it, but it’s really not elegant.

pit

UIMenuController is used. It is really a huge pit, the Internet search a variety of posts, can not solve the problem I encountered. In the end this post helped me.

1

UIMenuItem(title: “QQ”, Action: #selector(MQQ)) so if you look at this structure, it’s natural to go to MQQ first and this method is going to receive a UIMenuItem but it’s going to pass a UIMenuController

2

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let cell = tableView.cellForRow(at: indexPath) {
/ / self. BecomeFirstResponder () first comments here
            let qqItem = UIMenuItem(title: "QQ", action: #selector(mqq))
            let wechatItem = UIMenuItem(title: "wechat", action: #selector(wechat))
            let menuController = UIMenuController.shared
            menuController.menuItems = [qqItem, wechatItem]
            menuController.setTargetRect(cell.frame, in: cell.superview!)
            menuController.setMenuVisible(true, animated: true) selectedText = cell.textLabel? .text } }Copy the code

So this code right here in UITableViewController, if you click on the cell and you don’t switch the TAB, UIMenuController will show up, but it won’t show up. The one that has to be annotated

3

func longPress(sender: UILongPressGestureRecognizer) {
        if sender.state. = = began {self. BecomeFirstResponder () / / it is very importantlet menuController = UIMenuController.shared
            let item1 = UIMenuItem(title: 1 "test", action: #selector(test1))
            let item2 = UIMenuItem(title: "The test 2", action: #selector(test2))
            menuController.menuItems = [item1, item2]
            menuController.setTargetRect(frame, in: superview!)
            menuController.setMenuVisible(true, animated: true)}}Copy the code

On the basis of the above, put this code in the custom cell, UIMenuController is not displayed again must be added

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if [#selector(test1), #selector(test2)].contains(action) {
            return true
        }
        return false
    }Copy the code

You can tell from the method name that this is the method that can be executed, and it will display the corresponding UIMenuItem

If you do

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return true
    }Copy the code

It’s going to show the ones built in and the ones you defined, and if you don’t implement the corresponding Selector, it’s going to Crash





Screen Shot 2017-02-22 at 2.26.14.png





Screen Shot 2017-02-22 at 2.26.27.png





Screen Shot 2017-02-22 at 2.26.39.png





Screen Shot 2017-02-22 at 2.31.37.png

In this paper, the Demo