美文网首页大刘的 iOS 自学笔记
手势之 shouldReceive touch

手势之 shouldReceive touch

作者: 大刘 | 来源:发表于2022-06-15 12:20 被阅读0次

    Created by 大刘 liuxing8807@126.com

    在实际做项目中遇到一个小问题,即往父视图中添加UITapGestureRecognizer,即使点击了子视图,手势也会响应

    gesture.png

    如上,手势加在红色视图上,绿色视图添加到了红色视图上,点击绿色的子视图,触发了tap方法:

    import UIKit
    
    class ViewController: UIViewController {
        var redView: UIView = UIView()
        var greenView: UIView = UIView()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // red view
            redView.frame = self.view.bounds
            redView.backgroundColor = UIColor.red
            let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.tap))
            redView.addGestureRecognizer(tap)
            self.view.addSubview(redView)
            
            // green view on red view
            greenView.frame = CGRect.init(x: 0.0, y: self.view.bounds.size.height/2.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height/2.0)
            greenView.backgroundColor = UIColor.green
            greenView.isUserInteractionEnabled = true
            redView.addSubview(greenView);
        }
        
        @objc
        func tap() {
            print("YES")
        }
    }
    

    参考:stackoverflow, 需要实现代理方法 UIGestureRecognizerDelegate:

    import UIKit
    
    class ViewController: UIViewController, UIGestureRecognizerDelegate {
        var redView: UIView = UIView()
        var greenView: UIView = UIView()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // red view
            redView.frame = self.view.bounds
            redView.backgroundColor = UIColor.red
            let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.tap))
            tap.delegate = self; // 添加代理方法
            redView.addGestureRecognizer(tap)
            self.view.addSubview(redView)
            
            // green view on red view
            greenView.frame = CGRect.init(x: 0.0, y: self.view.bounds.size.height/2.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height/2.0)
            greenView.backgroundColor = UIColor.green
            redView.addSubview(greenView);
        }
        
        @objc
        func tap() {
            print("YES")
        }
        
        //MARK: -
        //实现代理方法
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
            // Prevent subviews of a specific view to send touch events to the view's gesture recognizers.
            if let touchedView = touch.view, let gestureView = gestureRecognizer.view, touchedView.isDescendant(of: gestureView), touchedView !== gestureView {
                // 假如触摸在绿色视图上:
                // touchedView: 触摸的视图,绿色视图
                // gestureView: 添加手势的视图,红色视图
                print("click green view")
                return false
            }
            print("click red view")
            return true
        }
    }
    

    相关文章

      网友评论

        本文标题:手势之 shouldReceive touch

        本文链接:https://www.haomeiwen.com/subject/wgkxvrtx.html