2017-04-25 63 views
0

在子视图上创建的UIImageView上的长按手势不会触发其目标。虽然,我已经启用了用户交互,但没有结果。 子视图以某种方式干涉和避免触发或我完全失去了一些东西? 我向社区提前致谢。Swift 3长按手势无法在子视图上工作

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

@mitul marsonia,谢谢,但没有工作。 – Odd

回答

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") 
     } 

    } 

} 

我的代码复制到一个新的项目,它的工作原理。

+0

感谢飞机!我知道它在单个应用程序上运行良好。这就是我特别提到“subView”方面的原因。 – Odd