on single tap UIBarButton display toast and on double tap back action need to perform

前端 未结 3 779
南旧
南旧 2021-01-27 08:24

I have an UIBarButton in navigation bar, while clicking on back button (first tap) i need to display toast (like warning), on double tap i need to exit from the page in swift,

相关标签:
3条回答
  • 2021-01-27 08:31

    Add an target-action on UIButton for the control event UIControlEventTouchDownRepeat, and do action only when the touch's tapCount is 2. Like below in Swift 3

    button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)
    

    And Then add selector as below

    func multipleTap(_ sender: UIButton, event: UIEvent) {
        let touch: UITouch = event.allTouches!.first!
        if (touch.tapCount == 2) {
            // do action.
        }
    }
    
    0 讨论(0)
  • 2021-01-27 08:42

    Use UIControlEventTouchDownRepeat event, and do action when tapCount is 2.

    0 讨论(0)
  • 2021-01-27 08:52
    override func viewDidLoad() {
            super.viewDidLoad()
    
            self.navigationItem.hidesBackButton = true
            let button = UIButton(type: .system)
            button.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
            button.setTitle("Back", for: .normal)
    
            //Gesture Recognizer
            let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
            singleTap.numberOfTapsRequired = 1
            let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
            doubleTap.numberOfTapsRequired = 2
            button.addGestureRecognizer(singleTap)
            button.addGestureRecognizer(doubleTap)
            singleTap.require(toFail: doubleTap)
    
            let barButton = UIBarButtonItem(customView: button)
            self.navigationItem.leftBarButtonItem = barButton
        }
        @objc func handleSingleTap(sender: UITapGestureRecognizer? = nil) {
            let toastLabel = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height-100, width: 350, height: 35))
            toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
            toastLabel.textColor = UIColor.white
            toastLabel.textAlignment = .center;
            toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
            toastLabel.text = "demo"
            toastLabel.alpha = 1.0
            toastLabel.layer.cornerRadius = 10;
            toastLabel.clipsToBounds  =  true
            self.view.addSubview(toastLabel)
            UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaseOut, animations: {
                toastLabel.alpha = 0.0
            }, completion: {(isCompleted) in
                toastLabel.removeFromSuperview()
            })
            print("Single Tap detected")
        }
        @objc func handleDoubleTap(sender: UITapGestureRecognizer? = nil) {
            print("Double Tap detected")
        }
    
    0 讨论(0)
提交回复
热议问题