2017-04-25 8 views
0

서브 뷰에서 생성 된 UIImageView의 길게 눌러 제스처가 해당 타겟을 트리거하지 않습니다. 비록 사용자 상호 작용을 이미 활성화했지만 아직 결과가 없습니다. 하위보기가 어떻게 든 방해하여 트리거링을 피합니까? 아니면 완전히 누락 된 것입니까? 커뮤니티에 미리 감사드립니다.스위프트 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