2017-07-21 144 views

回答

0

只需在下面添加 UITapGestureRecognizer到您的UIBarButtonItem。

func addBarButton() -> UIBarButtonItem 
{ 
     let btnImage = UIImage(named: "imageName")! 
     let imageButton : UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25)) 
     imageButton.setBackgroundImage(btnImage, for: UIControlState()) 

     let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.buttonAction(_:))) 
     doubleTap.numberOfTapsRequired = 2 
     imageButton.addGestureRecognizer(doubleTap) 

     return UIBarButtonItem(customView: imageButton) 
} 

從您的viewDidLoad函數中調用波紋管線。

navigationItem.rightBarButtonItems = [addBarButton()] 
1

UIBarButtonItem創建自定義視圖,並添加UITapGestureRecognizer自定義視圖,設置numberOfTapsRequired屬性值2

示例代碼:

class viewController: UIViewController { 

     var doubleTapGesture: UITapGestureRecognizer! 
     var barButton: UIBarButtonItem! 
     var button: UIButton! 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      button = UIButton(type: .custom) 
      button.setTitle("title", for: .normal) 
      button.frame = CGRect(x: 0, y: 0, width: 64, height: 30) 

      doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(viewController.buttonClicked)) 
      doubleTapGesture.numberOfTapsRequired = 2 

      button.addGestureRecognizer(doubleTapGesture) 

      barButton = UIBarButtonItem(customView: button); 
      navigationItem.rightBarButtonItem = barButton 
     } 

     func buttonClicked() { 
      // Method for UITapGestureRecognizer 
     } 
    } 
1

使用的UIButton代替的UIBarButtonItem和定義其「Touch Down Repeat」事件而不是「Touch Up Inside」。 觸按按鈕雙擊按鈕時重複觸發器。

檢查下面的圖像以供參考

enter image description here