代码之家  ›  专栏  ›  技术社区  ›  John

在多个UIImageView上点击一次手势[重复]

  •  1
  • John  · 技术社区  · 7 年前

    我有2个UIImageView和一个TapGestureRecognitor。

     override func viewDidLoad() {
            // Do any additional setup after loading the view.
    
            super.viewDidLoad()
    
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
    
            cameraUIImageView.isUserInteractionEnabled = true
            cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
    
            plus1UIImageView.isUserInteractionEnabled = true
            plus1UIImageView.addGestureRecognizer(tapGestureRecognizer)
    //        
    
    
        }
    

    我只能点击第二个UIImageView,即plus1UIImageView。

    3 回复  |  直到 7 年前
        1
  •  5
  •   Bilal hao zou    7 年前

    A. UIGestureRecognizer 只能与单个视图一起使用。两个视图使用的是同一个对象。试试这个。

    override func viewDidLoad() {
        // Do any additional setup after loading the view.
    
        super.viewDidLoad()
    
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
    
        cameraUIImageView.isUserInteractionEnabled = true
        cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
    
    
        let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
    
        plus1UIImageView.isUserInteractionEnabled = true
        plus1UIImageView.addGestureRecognizer(tapGestureRecognizer2)        
    }
    
        3
  •  0
  •   luckyShubhra    7 年前

    正如所有ans所述,您只能将手势识别器添加到一个视图中。虽然如果两个图像视图位于同一个超级视图中,您可以将点击手势添加到它们的超级视图并访问其子视图。您可以检查点击子视图,并使用 hitTest: here