2017-04-25 47 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

相關問題