Swift 遍历scrollView并找到单击的imageView
我有一个水平可滚动的滚动视图,其中有很多图像视图。我想突出显示用户单击的imageView,但我不确定如何执行此操作。我在图像中添加了一个点击手势:Swift 遍历scrollView并找到单击的imageView,swift,imageview,scrollview,uitapgesturerecognizer,Swift,Imageview,Scrollview,Uitapgesturerecognizer,我有一个水平可滚动的滚动视图,其中有很多图像视图。我想突出显示用户单击的imageView,但我不确定如何执行此操作。我在图像中添加了一个点击手势: let tap = UITapGestureRecognizer(target: self, action: #selector(imgTapped(_:))) 但是我不知道从这里开始在imgTapped函数中做什么。。。每个imageView都有一个唯一的标记 任何帮助都将不胜感激 如果您拥有所有图像视图的集合,您可以执行以下操作: func
let tap = UITapGestureRecognizer(target: self, action: #selector(imgTapped(_:)))
但是我不知道从这里开始在imgTapped
函数中做什么。。。每个imageView都有一个唯一的标记
任何帮助都将不胜感激 如果您拥有所有
图像视图的集合
,您可以执行以下操作:
func imgTapped(_ sender: UIGestureRecognizer) {
// get the tag for the clicked imageView
guard let tag = sender.view?.tag else { return }
let imageView = ImageViews.filter { $0.tag == tag }.first
}
否则,您可以遍历滚动视图子视图
,签出注释并查看代码中发生了什么:
func imgTapped(_ sender: UIGestureRecognizer) {
// get the tag for the clicked imageView
guard let tag = sender.view?.tag else { return }
// iterate through your scrollViews subviews
// and check if it´s an imageView
for case let imageView as UIImageView in self.imageScrollView.subviews {
// check if the tag matches the clicked tag
if imageView.tag == tag {
// this is the tag the user has clicked on
// highlight it here
}
}
}