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
-
Make your
tapGestureRecognizer
global or move it to yourloadImagePicker
function.将tapGestureRecognizer设为全局或将其移至loadImagePicker函数。
-
After setting
isUserInteractionEnabled
to true, add the gesture recogniser to theimageView
by callingimageView.addGestureRecognizer(tapGestureRecognizer)
.将isUserInteractionEnabled设置为true后,通过调用imageView.addGestureRecognizer(tapGestureRecognizer)将手势识别器添加到imageView。
- Then implement the
imageTapped
function which accepts a sender of typeUITapGestureRecognizer
- 然后实现imageTapped函数,该函数接受UITapGestureRecognizer类型的发送者
- Finally in the imageTapped function, access the UIImageView that was tapped by calling
sender as? UIImageView
- 最后在imageTapped函数中,访问通过调用发送者来点击的UIImageView?的UIImageView
#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
-
Make your
tapGestureRecognizer
global or move it to yourloadImagePicker
function.将tapGestureRecognizer设为全局或将其移至loadImagePicker函数。
-
After setting
isUserInteractionEnabled
to true, add the gesture recogniser to theimageView
by callingimageView.addGestureRecognizer(tapGestureRecognizer)
.将isUserInteractionEnabled设置为true后,通过调用imageView.addGestureRecognizer(tapGestureRecognizer)将手势识别器添加到imageView。
- Then implement the
imageTapped
function which accepts a sender of typeUITapGestureRecognizer
- 然后实现imageTapped函数,该函数接受UITapGestureRecognizer类型的发送者
- Finally in the imageTapped function, access the UIImageView that was tapped by calling
sender as? UIImageView
- 最后在imageTapped函数中,访问通过调用发送者来点击的UIImageView?的UIImageView