在单击UIBarButton显示吐司和双击背景动作需要执行

时间:2018-04-03 10:28:10

标签: ios swift3 navigationbar

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

以下代码用于显示吐司,其工作正常,

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

请指导我完成这项任务。

3 个答案:

答案 0 :(得分:0)

使用UIControlEventTouchDownRepeat事件,并在tapCount2时执行操作。

答案 1 :(得分:0)

在UIButton上为控制事件UIControlEventTouchDownRepeat添加目标操作,并仅在触摸的tapCount为2时执行操作。如下面的Swift 3

Input(s):
Successfully read 4 records (1090 bytes) from: "~/temp1.log"

Output(s):
Successfully stored 4 records (753 bytes) in: "hdfs://sandbox.hortonworks.com:8020/tmp/temp-53432852/tmp1982058168"

Counters:
Total records written : 4
Total bytes written : 753
Spillable Memory Manager spill count : 0
Total bags proactively spilled: 0
Total records proactively spilled: 0

Job DAG:
job_1522732012453_0054


2018-04-03 10:44:33,436 [main] INFO  org.apache.hadoop.yarn.client.api.impl.TimelineClientImpl - Timeline service address: http://sandbox.hortonworks.com:8188/ws/v1/timeline/
2018-04-03 10:44:33,436 [main] INFO  org.apache.hadoop.yarn.client.RMProxy - Connecting to ResourceManager at sandbox.hortonworks.com/172.17.0.2:8050
2018-04-03 10:44:33,437 [main] INFO  org.apache.hadoop.yarn.client.AHSProxy - Connecting to Application History server at sandbox.hortonworks.com/172.17.0.2:10200
2018-04-03 10:44:33,467 [main] INFO  org.apache.hadoop.mapred.ClientServiceDelegate - Application state is completed. FinalApplicationStatus=SUCCEEDED. Redirecting to job history server
2018-04-03 10:44:33,620 [main] INFO  org.apache.hadoop.yarn.client.api.impl.TimelineClientImpl - Timeline service address: http://sandbox.hortonworks.com:8188/ws/v1/timeline/
2018-04-03 10:44:33,620 [main] INFO  org.apache.hadoop.yarn.client.RMProxy - Connecting to ResourceManager at sandbox.hortonworks.com/172.17.0.2:8050
2018-04-03 10:44:33,620 [main] INFO  org.apache.hadoop.yarn.client.AHSProxy - Connecting to Application History server at sandbox.hortonworks.com/172.17.0.2:10200
2018-04-03 10:44:33,635 [main] INFO  org.apache.hadoop.mapred.ClientServiceDelegate - Application state is completed. FinalApplicationStatus=SUCCEEDED. Redirecting to job history server
2018-04-03 10:44:33,797 [main] INFO  org.apache.hadoop.yarn.client.api.impl.TimelineClientImpl - Timeline service address: http://sandbox.hortonworks.com:8188/ws/v1/timeline/
2018-04-03 10:44:33,797 [main] INFO  org.apache.hadoop.yarn.client.RMProxy - Connecting to ResourceManager at sandbox.hortonworks.com/172.17.0.2:8050
2018-04-03 10:44:33,799 [main] INFO  org.apache.hadoop.yarn.client.AHSProxy - Connecting to Application History server at sandbox.hortonworks.com/172.17.0.2:10200
2018-04-03 10:44:33,813 [main] INFO  org.apache.hadoop.mapred.ClientServiceDelegate - Application state is completed. FinalApplicationStatus=SUCCEEDED. Redirecting to job history server
2018-04-03 10:44:33,929 [main] INFO  org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher - Success!
2018-04-03 10:44:33,939 [main] INFO  org.apache.pig.data.SchemaTupleBackend - Key [pig.schematuple] was not set... will not generate code.
2018-04-03 10:44:33,955 [main] INFO  org.apache.hadoop.mapreduce.lib.input.FileInputFormat - Total input paths to process : 1
2018-04-03 10:44:33,955 [main] INFO  org.apache.pig.backend.hadoop.executionengine.util.MapRedUtil - Total input paths to process : 1

(123.125.67.216,-,-,02/Jan/2012:00:48:27 -0800,GET,/wiki/Dekart,HTTP/1.1,200,4512,-,Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2)
(209.85.238.130,-,-,02/Jan/2012:00:48:27 -0800,GET,/w/index.php?title=Special:RecentChanges&feed=atom,HTTP/1.1,304,260,-,Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; 4 subscribers; feed-id=11568779694056348047))
(123.125.67.213,-,-,02/Jan/2012:00:48:33 -0800,GET,/,HTTP/1.1,301,433,-,Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2)
(123.125.67.214,-,-,02/Jan/2012:00:48:33 -0800,GET,/wiki/Main_Page,HTTP/1.1,200,8647,-,Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2)

然后添加选择器,如下所示

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

答案 2 :(得分:0)

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