如何在Swift中识别UIImage数组中的Tapped UIImage

[英]How to Identify Tapped UIImage in UIImage array in Swift


I am populating UIImage array with code and adding them into UIScrollView successfully. I would like to identify the image tapped so I can run an action accordingly.

我用代码填充UIImage数组并成功地将它们添加到UIScrollView中。我想识别点击的图像,以便我可以相应地执行操作。

I appreciate your guidance and time.

感谢您的指导和时间。

Here is the code I have so far:

这是我到目前为止的代码:

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))
    }

    func loadImagePicker(){

    var contentWidth: CGFloat = 0.0
    let imageWidth: CGFloat = 100.0
    let imageHeight: CGFloat = 100.0

    for x in 0...2 {
        let image = UIImage(named: "item\(x).png")
        let imageView = UIImageView(image: image)
        imageView.isUserInteractionEnabled = true
        myData.images1.append(imageView)

        var newX: CGFloat = 0.0

        newX = 10 + imageWidth * CGFloat(x) + (10 * CGFloat(x))

        contentWidth += newX

        scrollView.addSubview(imageView)

        imageView.frame = CGRect(x: newX, y: scrollView.frame.size.height, width: imageWidth, height: imageHeight)
        imageView.contentMode = .scaleToFill
    }

    print(myData.images1.count)

    scrollView.clipsToBounds = false
    scrollView.contentSize = CGSize(width: contentWidth, height: scrollView.frame.size.height)

    imagePickerView.isHidden = false
}

2 个解决方案

#1


1  

Add gesture recognizer to your imageView just like following:

在您的imageView中添加手势识别器,如下所示:

    //Instance Variable
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))
    tapGestureRecognizer.numberOfTapsRequired = 1
    tapGestureRecognizer.numberOfTouchesRequired = 1

    func loadImagePicker(){

        // .....

        for 0..<2 { //Assuming there are two images

        // .....
        // .....

        imageView.addGestureRecognizer(tapGestureRecognizer)

        }
    }

    func imageTapped(_ sender: UITapGestureRecognizer) {

        let tappedImageView = sender.view as! UIImageView
        let index = tappedImageView.tag // Do anything with the index by using it in your array

    }

#2


0  

  1. Make your tapGestureRecognizer global or move it to your loadImagePicker function.

    将tapGestureRecognizer设为全局或将其移至loadImagePicker函数。

  2. After setting isUserInteractionEnabled to true, add the gesture recogniser to the imageView by calling imageView.addGestureRecognizer(tapGestureRecognizer).

    将isUserInteractionEnabled设置为true后,通过调用imageView.addGestureRecognizer(tapGestureRecognizer)将手势识别器添加到imageView。

  3. Then implement the imageTapped function which accepts a sender of type UITapGestureRecognizer
  4. 然后实现imageTapped函数,该函数接受UITapGestureRecognizer类型的发送者
  5. Finally in the imageTapped function, access the UIImageView that was tapped by calling sender as? UIImageView
  6. 最后在imageTapped函数中,访问通过调用发送者来点击的UIImageView?的UIImageView

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/06/08/ad6e9f1232b69cda6a456890efeb72cc.html



 
  © 2014-2022 ITdaan.com 联系我们: