在子视图上创建的UIImageView上的长按手势不会触发其目标。虽然,我已经启用了用户交互,但没有结果。 子视图是否以某种方式干扰并避免触发,或者我完全错过了什么? 我对社区提前表示感谢。
private func createShieldView() -> Void {
self.baseView.addSubview(self.imageForShield)
self.imageForShield.translatesAutoresizingMaskIntoConstraints = false
//Constraints functions created in an extension
self.imageForShield.horizontalLeft(toItem: self.alarmBaseView, constant: 0)
self.imageForShield.horizontalRight(toItem: self.alarmBaseView, constant: 0)
self.imageForShield.topConstraints(toItem: self.labelForInstruction, constant: 5, toBottomOf: true)
self.imageForShield.heightConstriants(constant: 250)
let imageFile = UIImage(named: "stateInactive")
self.imageForShield.image = imageFile
self.imageForShield.contentMode = UIViewContentMode.scaleAspectFit
self.imageForShield.tag = tagsAssignedToViews.shieldView.rawValue
let longPressEvent = UILongPressGestureRecognizer(target: self, action: #selector(ViewController._selectorLongPressEvent(longPressGestuer:)))
longPressEvent.minimumPressDuration = 2.0
self.imageForShield.addGestureRecognizer(longPressEvent)
self.imageForShield.isUserInteractionEnabled = true
}
//In ViewController.swift
public func _selectorLongPressEvent(longPressGestuer: UILongPressGestureRecognizer) -> Void {
if longPressGestuer.state == UIGestureRecognizerState.began {
print("Long press event triggered")
}
}
答案 0 :(得分:0)
我测试你的代码(除了约束),如下所示,它工作,它打印信息,试图找到问题:
import UIKit
class ViewController: UIViewController {
lazy var baseView:UIView = {
let view:UIView = UIView.init()
view.frame = CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
return view
}()
lazy var imageForShield:UIImageView = {
let imageView:UIImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 330, height: 330))
imageView.backgroundColor = UIColor.red
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.createShieldView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func createShieldView() -> Void {
self.view.addSubview(self.baseView)
self.baseView.addSubview(self.imageForShield)
self.imageForShield.translatesAutoresizingMaskIntoConstraints = false
//Constraints functions created in an extension
/*
self.imageForShield.horizontalLeft(toItem: self.alarmBaseView, constant: 0)
self.imageForShield.horizontalRight(toItem: self.alarmBaseView, constant: 0)
self.imageForShield.topConstraints(toItem: self.labelForInstruction, constant: 5, toBottomOf: true)
self.imageForShield.heightConstriants(constant: 250)
*/
let imageFile = UIImage(named: "stateInactive")
self.imageForShield.image = imageFile
self.imageForShield.contentMode = UIViewContentMode.scaleAspectFit
//self.imageForShield.tag = tagsAssignedToViews.shieldView.rawValue
let longPressEvent = UILongPressGestureRecognizer(target: self, action: #selector(ViewController._selectorLongPressEvent(longPressGestuer:)))
longPressEvent.minimumPressDuration = 2.0
self.imageForShield.addGestureRecognizer(longPressEvent)
self.imageForShield.isUserInteractionEnabled = true
}
//In ViewController.swift
public func _selectorLongPressEvent(longPressGestuer: UILongPressGestureRecognizer) -> Void {
if longPressGestuer.state == UIGestureRecognizerState.began {
print("Long press event triggered")
}
}
}
将我的代码复制到一个新项目,它可以工作。