ios Swift 4 - 将 UITapGestureRecognizer 添加到子视图图像 - 该方法未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46583964/
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 4 - Adding UITapGestureRecognizer to a subview image - the method is not called
提问by Chris G.
I have the following UITapGestureRecognizer
setup, but the method is not called?
我有以下UITapGestureRecognizer
设置,但没有调用该方法?
Note: that the UITapGestureRecognizer
is added to a subview item.
注意:UITapGestureRecognizer
添加到子视图项中。
Also, it works when adding the UIGestureRecognizerDelegate
in SUStepView
itself - only problem is that I need it in the container.
此外,添加工作时UIGestureRecognizerDelegate
在SUStepView
本身-唯一的问题是,我需要它在容器中。
class StepViewContainer: NSObject, UIGestureRecognizerDelegate {
var view: SUStepView?
@objc func tapAction(recognizer: UITapGestureRecognizer) {
}
override init(){
super.init()
// View
self.view = Bundle.main.loadNibNamed("SignupV3Views", owner: self, options: nil)![0] as? SUStepView
let mytapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
mytapGestureRecognizer.numberOfTapsRequired = 1
self.view?.imageView.addGestureRecognizer(mytapGestureRecognizer)
}
}
The view in StepViewContainer:
StepViewContainer 中的视图:
class SUStepView: UIView {
@IBOutlet weak var imageView: UIImageView!
@objc public func nextStepTap(sender: UITapGestureRecognizer) {
}
override func awakeFromNib() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(nextStepTap))
tapGestureRecognizer.numberOfTapsRequired = 1
self.imageView.addGestureRecognizer(tapGestureRecognizer)
self.imageView.isUserInteractionEnabled = true
self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true
}
回答by Kirit Modi
Swift 4 Code :
斯威夫特 4 代码:
TapGesture :
点击手势:
tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
ImageView Tap :
图像视图点击:
self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width/2.0, height: self.view.frame.size.height/2.0))
self.imageView.isUserInteractionEnabled = true
self.imageView.backgroundColor = UIColor.red
self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true
self.imageView.isUserInteractionEnabled = true
self.imageView.addGestureRecognizer(tapGesture)
self.view.addSubview(self.imageView)
Call On Tap
点按呼叫
@objc func myviewTapped(_ sender: UITapGestureRecognizer) {
if self.imageView.backgroundColor == UIColor.yellow {
self.imageView.backgroundColor = UIColor.green
}else{
self.imageView.backgroundColor = UIColor.yellow
}
}
回答by Dixit Akabari
try this.
尝试这个。
let mytapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(myTapAction))
mytapGestureRecognizer.numberOfTapsRequired = 1
self.imageView.addGestureRecognizer(mytapGestureRecognizer)
method.
方法。
@objc func myTapAction(recognizer: UITapGestureRecognizer) {
}