我正在处理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)


ios - UIMenuController和TapGesture在移动时重新出现-LMLPHP

最佳答案

好吧,终于我这样解决了:

迅捷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
}

关于ios - UIMenuController和TapGesture在移动时重新出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45295669/

10-10 14:35