UIMenuController在项目中的使用

本文介绍了一种在Swift中实现消息长按复制的方法。通过定义类型别名和辅助块来处理不同类型的聊天消息,例如文本、图片等,并且通过上下文菜单提供复制选项。此外,还展示了如何设置辅助块来响应特定的消息类型。

//BCmessageCell文件代码

typealias BCChatCellAuxiliarytypeBlock = (cell:BCMessageCell,message:ArrowIMMessage,type:BCChatCellType)->Void
var _auxiliaryBlock:BCChatCellAuxiliarytypeBlock?
func longpressBubbleView(){
        self.becomeFirstResponder()
        let menuController = UIMenuController.sharedMenuController()
        let menuItem_1 = UIMenuItem.init(title: "复制", action: Selector("copyChatMessage"))
//        let menuItem_2 = UIMenuItem.init(title: "删除", action: Selector("deleteChatMessage"))
//        let menuItem_3 = UIMenuItem.init(title: "收藏", action: Selector("saveChatMessage"))
//        let menuItem_4 = UIMenuItem.init(title: "转发", action: Selector("deliverChatMessage"))
        
        menuController.menuItems = [menuItem_1]
        menuController.setTargetRect((bubbleView?.frame)! , inView: self)
        menuController.setMenuVisible(true, animated: false)
//        delegate?.longPressBubbleView!(messageModel!)
    }
    func copyChatMessage(){
        if ((_auxiliaryBlock) != nil){
            _auxiliaryBlock!(cell: self,message: (self.messageModel?.message)!,type: .BCChatCellCopyStyle)
        }
    }
    
    func setAuxiliarytypeBlock(auxiliarytypeBlock:BCChatCellAuxiliarytypeBlock){
        _auxiliaryBlock = auxiliarytypeBlock
    }
    
    override func canBecomeFirstResponder()->Bool
    {
        return true
    }
    
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
            return action == Selector("copyChatMessage")
    }

  

//BCMessageViewController代码

写在func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 代理中
cell?.setAuxiliarytypeBlock({ (cell, message, type) -> Void in
                switch(type){
                case .BCChatCellCopyStyle:
                    let pasterBoard = UIPasteboard.generalPasteboard()
                    switch (message.messageFormat){
                    case .Text:
                        pasterBoard.string = message.context
                    case .Image: break
                    case .Audio: break
                    case .Video: break
                    case .File: break
                    case .Json: break
                    case .Location: break
                    case .At:
                        pasterBoard.string = message.context
                    default:break
                    
                    }
                case .BCChatCellDeleteStyle:break
                case .BCChatCellCollectionStyle:break
                case .BCChatCellDeliverStyle:break
                case .BCChatEarPhoneStyle:break
                }
            })

  

转载于:https://www.cnblogs.com/torrescx/p/5345786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值