iOS Swift: 从 UIImageView 的子类发送动作(控制事件)
iOS Swift: Send action (control event) from subclass of UIImageView
如何从 UIImageView 的子 class 向视图控制器发送动作(类似点击事件)。
Here is reference answer,表示我要申请UIImageView。 (UIImageView 在其超级 class 层次结构中没有 UIControl)
以下是我的代码,但无法正常工作,我不知道如何实现,我需要什么。
class TappableImageView: UIImageView {
// Initializer methods.....
//------------------------------------------
private func addTapGesture(){
let tapOnImage = UITapGestureRecognizer(target: self, action: #selector(TappableImageView.handleTapGesture(tapGesture:)))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(tapOnImage)
}
//----------------------------------------------
@objc func handleTapGesture(tapGesture: UITapGestureRecognizer) {
// how can I send tap/click event to all view controllers, where I've used this image from this point.
}
}
有没有适合我的alternate/other解决方案?
我会推荐你使用闭包来处理水龙头:
class TappableImageView: UIImageView {
var handleTap: (() -> Void)? = nil
//------------------------------------------
private func addTapGesture(){
let tapOnImage = UITapGestureRecognizer(target: self, action: #selector(TappableImageView.handleTapGesture(tapGesture:)))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(tapOnImage)
}
//----------------------------------------------
@objc func handleTapGesture(tapGesture: UITapGestureRecognizer) {
handleTap?()
}
}
然后我是你的视图控制器,你可以使用它,即在 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
yourTappableImageView.handleTap = {
print("an image view was tapped")
}
}
假设您的 TappableImageView
存储在名为 yourTappableImageView
的变量中。
如何从 UIImageView 的子 class 向视图控制器发送动作(类似点击事件)。
Here is reference answer,表示我要申请UIImageView。 (UIImageView 在其超级 class 层次结构中没有 UIControl)
以下是我的代码,但无法正常工作,我不知道如何实现,我需要什么。
class TappableImageView: UIImageView {
// Initializer methods.....
//------------------------------------------
private func addTapGesture(){
let tapOnImage = UITapGestureRecognizer(target: self, action: #selector(TappableImageView.handleTapGesture(tapGesture:)))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(tapOnImage)
}
//----------------------------------------------
@objc func handleTapGesture(tapGesture: UITapGestureRecognizer) {
// how can I send tap/click event to all view controllers, where I've used this image from this point.
}
}
有没有适合我的alternate/other解决方案?
我会推荐你使用闭包来处理水龙头:
class TappableImageView: UIImageView {
var handleTap: (() -> Void)? = nil
//------------------------------------------
private func addTapGesture(){
let tapOnImage = UITapGestureRecognizer(target: self, action: #selector(TappableImageView.handleTapGesture(tapGesture:)))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(tapOnImage)
}
//----------------------------------------------
@objc func handleTapGesture(tapGesture: UITapGestureRecognizer) {
handleTap?()
}
}
然后我是你的视图控制器,你可以使用它,即在 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
yourTappableImageView.handleTap = {
print("an image view was tapped")
}
}
假设您的 TappableImageView
存储在名为 yourTappableImageView
的变量中。