iOS / Swift:如何为backButton实现doubletap操作?

时间:2018-04-06 04:14:54

标签: ios swift uinavigationbar

我正在尝试为我的应用中的每个ViewController实现自定义后退按钮。我希望它有两个动作。如果按钮被单击,它应该给予警告。如果按钮被双击,它应该返回ViewController。

我怎样才能在swift中只为后退按钮实现这一点?

3 个答案:

答案 0 :(得分:0)

您无法修改BackBarButtonItem的操作和行为,您可以创建自己的UIBarButtonObject并将其指定为leftBarButtonItem,同时为其提供自己的选择器

需要注意的是,这将删除库存后退按钮图像。

答案 1 :(得分:0)

试试这个:

let doubleTap = UITapGestureRecognizer(target: self, action: #selector(actingFunction))
    DoubleTap.numberOfTapsRequired = 2
    barButton.addGestureRecognizer(doubleTap)

答案 2 :(得分:0)

尝试使用单按钮和双击按钮

/// Tap Gesture to Handle Double Tap
let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(SourceVC.handleGesture(getsure:)))
        tapGesture.numberOfTapsRequired = 2

/// Custom Button That With Handle two actions
let customButtonn : UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
customButtonn.setTitleColor(UIColor.blue, for: .normal)
customButtonn.setTitle("button", for: .normal)
customButtonn.addTarget(self, action: #selector(SourceVC.navButton(sender:)), for: .touchUpInside)
customButtonn.addGestureRecognizer(tapGesture)

/// Create a Custom bar Button  
let navLeftButton = UIBarButtonItem()
navLeftButton.customView = customButtonn

/// Assign it to navigation Bar
self.navigationItem.leftBarButtonItem = navLeftButton

我在SourceVC类中的自定义按钮操作

/// Single Tap
@objc func navButton(sender:UIButton){
        print("navButtonTapped Once")
      }

/// Double Tap
@objc func handleGesture(getsure:UITapGestureRecognizer){
        print("navButtonTapped twice")
      }

模拟器输出: enter image description here

控制台输出: enter image description here

注意 - 执行此操作会使用条形图移动默认值,您需要将其设计为自定义方式,因此它看起来像模拟器输出中显示的那样 - 按钮显示没有该背面图像