ios Swift 3 - 带有点击手势的图像视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42161476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 - Image view with Tap Gesture
提问by Chad
Im having issues with using a tap gesture that I have put on an image view. The image is currently stored in the Assets as 'ActionLiked' and I have set the image view to this image. It is then rendered into a table view which is dynamic based on JSON (so it repeats for each item I put into a JSON array). I added the tap gesture to print out 'TAPPED' each time I click on it however, it seems to not be working all the time - 7 items currently in the table, the tap gesture will work on 1 then not work on the next 2 then work on the 4th one and repeat that pattern
我在使用已放在图像视图上的点击手势时遇到问题。该图像当前作为“ActionLiked”存储在资产中,并且我已将图像视图设置为该图像。然后它被渲染成一个基于 JSON 的动态表视图(所以它对我放入 JSON 数组的每个项目重复)。我添加了点击手势以在每次点击它时打印出“TAPPED”但是,它似乎并没有一直工作 - 当前表格中有 7 个项目,点击手势将在 1 上工作,然后在接下来的 2 上不起作用然后在第四个工作并重复该模式
ITEM 1 - WORK ITEM 2 - NO WORK ITEM 3 - NO WORK ITEM 4 - WORK ITEM 5 - NO WORK ITEM 6 - NO WORK ITEM 7 - WORK
项目 1 - 工作项目 2 - 没有工作项目 3 - 没有工作项目 4 - 工作项目 5 - 没有工作项目 6 - 没有工作项目 7 - 工作
I get an error in my debug console Could not load the "" image referenced from a nib in the bundle with identifier But the image is showing correctly on each one just not recognising the tap gesture?
我在调试控制台中遇到错误无法加载从带有标识符的包中的笔尖引用的“”图像但是图像在每个人上都正确显示只是无法识别点击手势?
回答by Maulik Pandya
Following code may help you more with Swift 4.
以下代码可以帮助您更多地使用 Swift 4。
As you said you want to detect image tap on tableview cell please go through this code:
正如您所说,您想检测 tableview 单元格上的图像点击,请通过以下代码:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.cellTappedMethod(_:)))
cell.yourImageView.isUserInteractionEnabled = true
cell.yourImageView.tag = indexPath.row
cell.yourImageView.addGestureRecognizer(tapGestureRecognizer)
And add below method to your ViewController:
并将以下方法添加到您的 ViewController:
@objc func cellTappedMethod(_ sender:AnyObject){
print("you tap image number: \(sender.view.tag)")
}
回答by Muhammad Raza
Please check isUserInteractionEnabled
of UIImageView
is true
请检查isUserInteractionEnabled
的UIImageView
就是true
回答by Joey deVilla
I recently had an issue that seems similar to yours. I had a number of images, all of which I wanted to respond in the same way whenever the user tapped them. After some experimenting, it became clear to me that each image had to have its own UITapGestureRecognizer
instance. I ended up using code like this, which ensured that every image responded to being tapped:
我最近遇到了一个与您类似的问题。我有许多图像,当用户点击它们时,我希望以相同的方式响应所有图像。经过一些实验,我清楚地知道每个图像都必须有自己的UITapGestureRecognizer
实例。我最终使用了这样的代码,它确保每个图像都响应被点击:
for imageView in imageViews {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapResponse))
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.isUserInteractionEnabled = true
}
回答by Aleksandr Honcharov
The idea is that you should create unique gesture recognizer for every UIImageView.
这个想法是你应该为每个 UIImageView 创建独特的手势识别器。
let gestureRecognizerOne = UITapGestureRecognizer(target: self, action: #selector(tap))
firstImageView.addGestureRecognizer(gestureRecognizerOne)
let gestureRecognizerTwo = UITapGestureRecognizer(target: self, action: #selector(tap))
secondImageView.addGestureRecognizer(gestureRecognizerTwo)
But I didn't see your code, so, maybe you should create it in a loop or something like that.
但我没有看到你的代码,所以,也许你应该在循环或类似的东西中创建它。