2017-07-25 50 views
0

我正在處理UITableView上的UITapGesture,並在選定的UITableViewCell上顯示UIMenuController。當我選擇並出現菜單時,如果我將手指移動到單元格上,似乎它不斷出現的菜單並且我不太喜歡它的外觀。UIMenuController和TapGesture在運動中重新出現

有一些方法可以避免這種情況?

謝謝!

點擊動作代碼(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

回答

0

那麼最後我已經解決了這樣的:

斯威夫特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 
}