在UIScrollView中检测UIImageView上的点击

时间:2018-10-14 09:37:38

标签: ios swift uiscrollview

我正在尝试在UIScrollView中注册一个水龙头,但似乎无法使其正常工作。我尝试在这里找到的所有内容均未成功。我正在使用UIImageView的子类向其添加标识符,这不应该是问题。似乎也无法使其与默认的UIImageView类一起使用。无论我做什么,“ didTapImage”都不会执行。

func fillSelectionView(){
    let imageNameArray = ["entdecken", "erleben", "erinnern"]

    imageArray = [UIImage(named: imageNameArray[0]), UIImage(named: imageNameArray[1]), UIImage(named: imageNameArray[2])] as! [UIImage]

    for i in 0..<imageArray.count{
        let imageView = UIImageViewWithIdentifier()
        imageView.image = imageArray[i]
        imageView.contentMode = .scaleAspectFit
        imageView.setIdentifier(ident: imageNameArray[i])
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: Selector(("didTapImage"))))


        let xPosition = self.selectionView.frame.width * CGFloat(Double(i))
        imageView.frame = CGRect(x: xPosition, y: 0, width: selectionView.frame.width, height: selectionView.frame.height)
        selectionView.contentSize.width = self.selectionView.frame.width * CGFloat((Double(i) + 1))
        selectionView.addSubview(imageView)
    }
}

func didTapImage(gesture: UIGestureRecognizer) {
    let point = gesture.location(in: gesture.view)
    let imageViewTouched = gesture.view as! UIImageViewWithIdentifier
    let imageViewIdent = imageViewTouched.getIdentifier()

    print(point)
    print("touched" + imageViewIdent)
}

1 个答案:

答案 0 :(得分:-1)

您应该使用UITapGestureRecognizer