最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中双击和单击冲突

Swift crifan 3200浏览 0评论

本来有了双击代码的识别:

        let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTapped:")
        doubleTap.numberOfTapsRequired = 2
        doubleTap.numberOfTouchesRequired = 1
        self.scrollView.addGestureRecognizer(doubleTap)
    func doubleTapped(recognizer: UITapGestureRecognizer) {
        if (self.scrollView.zoomScale == self.scrollView.maximumZoomScale) {
            self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true)
        } else {
            let pointInView = recognizer.locationInView(self.imageView)
//            print("pointInView=\(pointInView)") //(898.155653734425, 425.882443769075)
           
            var newZoomScale = scrollView.zoomScale * 1.5
//            print("newZoomScale=\(newZoomScale)") //0.986187634979701
           
            newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)
//            print("newZoomScale=\(newZoomScale)") //0.986187634979701
           
            let scrollViewSize = scrollView.bounds.size
//            print("scrollViewSize=\(scrollViewSize)") //(375.0, 603.0)
           
            let w = scrollViewSize.width / newZoomScale
            let h = scrollViewSize.height / newZoomScale
            let x = pointInView.x – (w / 2.0)
            let y = pointInView.y – (h / 2.0)
           
            let rectToZoomTo = CGRectMake(x, y, w, h);
//            print("rectToZoomTo=\(rectToZoomTo)") //(708.029562766088, 120.159689491989, 380.252181936674, 611.445508554172)
           
            scrollView.zoomToRect(rectToZoomTo, animated: true)
        }
    }

可以正常工作

但是加了单击后:

        let singleTap = UITapGestureRecognizer(target: self, action: "singleTap:")
        singleTap.numberOfTapsRequired = 1
        singleTap.numberOfTouchesRequired = 1
        self.scrollView.addGestureRecognizer(singleTap)
    func singleTap(recognizer:UITapGestureRecognizer){
        if recognizer.state == UIGestureRecognizerState.Ended {
            self.navigationController?.popViewControllerAnimated(true)
        }
    }

结果:

双击时,第一次的点击,就触发了单击,导致双击失效。

搜:

swift single double tap 

swift single double tap conflict

iphone – How to recognize oneTap/doubleTap at moment? – Stack Overflow

结果试了半天,也没搞定:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
        if (touches.count == 2) {
            //This will cancel the singleTap action
            NSObject.cancelPreviousPerformRequestsWithTarget(self)
        }
    }
   
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (touches.count == 1) {
            singleTap()
        } else if (touches.count == 2) {
            if let firsTouch = touches.first {
                doubleTapped(firsTouch)
            }
        }
    }

参考:

UIGestureRecognizer in Swift

去搜:

requireGestureRecognizerToFail

参考:

IOS开发之手势——UIGestureRecognizer 共存 – 松花江以南 – 博客园

只需要加一行:

        singleTap.requireGestureRecognizerToFail(doubleTap)

成为:

        let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTapped:")
        doubleTap.numberOfTapsRequired = 2
        doubleTap.numberOfTouchesRequired = 1
        self.scrollView.addGestureRecognizer(doubleTap)
       
        let singleTap = UITapGestureRecognizer(target: self, action: "singleTap:")
        singleTap.numberOfTapsRequired = 1
        singleTap.numberOfTouchesRequired = 1
        self.scrollView.addGestureRecognizer(singleTap)
        singleTap.requireGestureRecognizerToFail(doubleTap)
    func doubleTapped(recognizer: UITapGestureRecognizer) {
        if (self.scrollView.zoomScale == self.scrollView.maximumZoomScale) {
            self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true)
        } else {
            let pointInView = recognizer.locationInView(self.imageView)
//            print("pointInView=\(pointInView)") //(898.155653734425, 425.882443769075)
           
            var newZoomScale = scrollView.zoomScale * 1.5
//            print("newZoomScale=\(newZoomScale)") //0.986187634979701
           
            newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)
//            print("newZoomScale=\(newZoomScale)") //0.986187634979701
           
            let scrollViewSize = scrollView.bounds.size
//            print("scrollViewSize=\(scrollViewSize)") //(375.0, 603.0)
           
            let w = scrollViewSize.width / newZoomScale
            let h = scrollViewSize.height / newZoomScale
            let x = pointInView.x – (w / 2.0)
            let y = pointInView.y – (h / 2.0)
           
            let rectToZoomTo = CGRectMake(x, y, w, h);
//            print("rectToZoomTo=\(rectToZoomTo)") //(708.029562766088, 120.159689491989, 380.252181936674, 611.445508554172)
           
            scrollView.zoomToRect(rectToZoomTo, animated: true)
        }
    }
   
    func singleTap(recognizer:UITapGestureRecognizer){
        if recognizer.state == UIGestureRecognizerState.Ended {
            //self.navigationController?.popViewControllerAnimated(true)
            self.navigationController?.popViewControllerAnimated(false)
        }
    }

即可。

转载请注明:在路上 » [已解决]swift中双击和单击冲突

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.199 seconds, using 22.19MB memory