UIMenuController和TapGesture重新出现在运动中

时间:2017-07-25 06:39:13

标签: ios swift xcode uitapgesturerecognizer uimenucontroller

我正在UITapGesture处理UITableView并在所选UIMenuController上显示UITableViewCell。当我选择并出现菜单时,如果我将手指移到单元格上,看起来它的菜单似乎不断出现,我不喜欢它看起来如何。

有一些方法可以避免这种情况吗?

谢谢!

点按手势代码(Swift 2.3):

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
longPressGesture.minimumPressDuration = 0.5 
longPressGesture.delegate = self
self.messagesTableView.addGestureRecognizer(longPressGesture)

enter image description here

1 个答案:

答案 0 :(得分:0)

好吧我终于解决了这个问题:

Swift 2

声明此全局变量

var menuControllerOpened:Bool = false

在tagGestureHandler上创建一个块:

func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    if !self.menuControllerOpened {
        let p = longPressGesture.locationInView(self.messagesTableView)
        self.selectedIndexPathMenu = self.messagesTableView.indexPathForRowAtPoint(p)
        if self.selectedIndexPathMenu != nil {
            //Do something you want
            let menu = UIMenuController.sharedMenuController()
            let reportItem = UIMenuItem(title: "Reportar usuario", action: #selector(reportMessageItem))
            menu.menuItems = [reportItem]
            menu.setTargetRect(cell.frame, inView: self.messagesTableView)
            menu.setMenuVisible(true, animated: true)
        }
    }

}

但是配置一些通知事件,它们将在显示或隐藏MenuController时处理:

func configureMenuControllerEvents() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleMenuControllerShow), name: UIMenuControllerWillShowMenuNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleMenuControllerHide), name: UIMenuControllerWillHideMenuNotification, object: nil)

}

func handleMenuControllerShow() {
    self.menuControllerOpened = true
}

func handleMenuControllerHide() {
    self.menuControllerOpened = false
}