在单击UIBarButton显示吐司上和在双击后动作上都需要执行 [英] on single tap UIBarButton display toast and on double tap back action need to perform

查看:85
本文介绍了在单击UIBarButton显示吐司上和在双击后动作上都需要执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航栏中有一个UIBarButton,在单击后退按钮(第一次点击)时,我需要显示吐司(如警告),在双击时,我需要迅速退出页面,

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,

以下用于显示吐司面包的代码,它的工作正常,

Following codes used for displaying toast, its working fine,

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()
    })

请指导我完成这项任务.

please guide me to acheive this task.

推荐答案

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")
    }

这篇关于在单击UIBarButton显示吐司上和在双击后动作上都需要执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆