1. 程式人生 > >Swift 手勢單擊,雙擊,長按

Swift 手勢單擊,雙擊,長按

    // MARK:tapGestureRecognizer手勢之單擊

    private func tapGestureRecognizerAction(){

        let rect = CGRect(x: 32, y: 80, width: 256, height: 256)

        let image = UIImage(named: "image")

        let imageView = UIImageView(frame: rect)

        imageView.image = image

        self.view.addSubview(imageView)

        imageView.isUserInteractionEnabled = true

        let guesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap(guesTure:)))

        imageView.addGestureRecognizer(guesture)

    }

    @objc private func singleTap( guesTure : UITapGestureRecognizer ){

        print(guesTure.numberOfTapsRequired)

        print(guesTure.numberOfTouchesRequired)

        let aleartView = UIAlertController(title:"Infomantion", message: "single Tap", preferredStyle: UIAlertController.Style.alert)

        let OKAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) { (UIAlertAction) in

        }

        aleartView.addAction(OKAction)

        self.present(aleartView, animated: true, completion: nil)    }

    // MARK:tapGestureRecognizer手勢之長按

    private func longPressGestureRecognizerAction(){

        let rect = CGRect(x: 32, y: 80, width: 256, height: 256)

        let image = UIImage(named: "image")

        let imageView = UIImageView(frame: rect)

        imageView.image = image

        view.addSubview(imageView)

        imageView.isUserInteractionEnabled = true

        let longGress = UILongPressGestureRecognizer()

        longGress.addTarget(self, action: #selector(longGressGestrue(longGress:)))

        imageView.addGestureRecognizer(longGress)

    }

    @objc private func longGressGestrue(longGress:UILongPressGestureRecognizer){

        if longGress.state == UIGestureRecognizer.State.began {

            print("began")

            let aleatVC = UIAlertController(title: "", message: "長按", preferredStyle: UIAlertController.Style.alert)

            let actionOK = UIAlertAction(title: "好的", style: UIAlertAction.Style.default) { (UIAlertAction) in

            }

            aleatVC.addAction(actionOK)

            self.present(aleatVC, animated: true, completion: nil)

        }else if longGress.state == UIGestureRecognizer.State.possible{

            print("possible")

        }else if longGress.state == UIGestureRecognizer.State.changed{

            print("changed")

        }else if longGress.state == UIGestureRecognizer.State.possible{

            print("possible")

        }else if longGress.state == UIGestureRecognizer.State.ended{

            print("ended")

        }else{

            print("cancelled")

        }

    }

    // MARK:tapGestureRecognizer手勢之雙擊

    private func doubleTapGestureRecognizerAction(){

        let rect = CGRect(x: 32, y: 80, width: 256, height: 256)

        let image = UIImage(named: "image")

        let imageView = UIImageView(frame: rect)

        imageView.image = image

        view.addSubview(imageView)

        imageView.isUserInteractionEnabled = true

        let doubleTap = UITapGestureRecognizer()

        doubleTap.numberOfTapsRequired = 2

        doubleTap.numberOfTouchesRequired = 1;

        doubleTap.addTarget(self, action: #selector(doubleTapGesture(doubleTapGesture:)))

        imageView.addGestureRecognizer(doubleTap)

    }

    @objc private func doubleTapGesture(doubleTapGesture:UITapGestureRecognizer){

        let aleartVC = UIAlertController(title: "infoMation", message: "雙擊", preferredStyle: UIAlertController.Style.alert)

        let actionOK = UIAlertAction(title: "好的", style: UIAlertAction.Style.default) { (UIAlertAction) in

        }

        aleartVC.addAction(actionOK)

        self.present(aleartVC, animated: true, completion: nil)

    }