那麼最後我已經解決了這樣的:
斯威夫特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
}