2017-05-22 5 views
0
@IBAction func popView(_ sender: UIButton) { 
    let popView = PopView() 
    popView.setVisible() 
    popView.animateView(view: popView.contentView) 

} 

popViewはビューをいくつか持つviewcontrollerで、popViewはinit()でこれらのビューを設定するためにsetup()を使用します。UITapGestureRecognizerが別のコントローラに属しているビューでは機能しない

func setup(){ 
    view.frame = UIScreen.main.bounds 

    baseView.frame = view.frame 
    baseView.backgroundColor = UIColor.black 
    //baseView.alpha = appearence.backgroundAlpha 
    view.addSubview(baseView) 

    contentView.backgroundColor = UIColor.white 
    contentView.layer.cornerRadius = appearence.cornerRadius 
    contentView.frame = CGRect(x: 10, y: 10, width: appearence.width, height: appearence.height) 
    contentView.layer.masksToBounds = true 
    contentView.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY) 


    baseView.addSubview(contentView) 

    headView.frame = CGRect(x: 0, y: 0, width: contentView.bounds.width, height: contentView.bounds.height/2) 
    headView.backgroundColor = UIColorFromRGB(0xE64D4D) 
    contentView.addSubview(headView) 



    headImageView.image = UIImage(named: headImage) 
    headImageView.contentMode = .center 
    headImageView.frame.size = headImageView.image!.size 
    headImageView.center = CGPoint(x: headView.bounds.midX, y: headView.bounds.midY) 
    headView.addSubview(headImageView) 



    let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.hide(_:))) 
    recognizer.numberOfTapsRequired = 1 
    contentView.addGestureRecognizer(recognizer) 
    print(String(describing: contentView.gestureRecognizers)) 
} 

contentViewがtapgesturerecognizerを持っていますが、popViewが

@IBAction func popView(_ sender: UIButton) { 
    let popView = PopView() 
    popView.setVisible() 
    popView.animateView(view: popView.contentView) 

} 

func setVisible(){ 
    let rootView = UIApplication.shared.keyWindow! as UIWindow 
    rootView.addSubview(self.view) 

} 

func animateView(view: UIView){ 
    view.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.minY - view.bounds.midY) 
    UIView.animate(withDuration: 0.2, animations: { 
     view.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY + 50) 
    }, completion: {(finished) -> Void in 
     UIView.animate(withDuration: 0.1, animations: { 
      view.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY) 

     }) 


    }) 
} 

のViewControllerクラスに以下のコードによって作成された

func hide(_ recognizer: UITapGestureRecognizer){ 
    self.view.removeFromSuperview() 
} 

私はタップしたとき、それは動作しませんpopViewが正しく表示されますが、contentViewタップジェスチャーに応答しません。なぜ?助けてください〜

答えて

0

コンテンツビューの代わりにpopViewのビューコントローラにタップジェスチャーを追加してください。以下のように動作します。

let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.hide(_:))) 
     recognizer.numberOfTapsRequired = 1 
     popView.view.addGestureRecognizer(recognizer) 
     print(String(describing: popView.view.gestureRecognizers)) 

ボタンアクションクラスにも移動します。それを隠すと、そのクラス自体に以下のように表示されます。

func hide(_ recognizer: UITapGestureRecognizer){ 
     self.popView.view.removeFromSuperview() 

     let rootView = UIApplication.shared.keyWindow! as UIWindow 
     rootView.addSubview(self.view) 


    } 

これが役立ちます。

以下

は私のPopViewクラスのコードです:

import UIKit 

class PopView: UIViewController { 

    var baseView: UIView = UIView() 
    var contentView: UIView = UIView() 
    var headView: UIView = UIView() 
    var headImageView: UIImageView = UIImageView() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     self.setup() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func setup(){ 
     view.frame = UIScreen.main.bounds 

     baseView.frame = view.frame 
     baseView.backgroundColor = UIColor.black 
     //baseView.alpha = appearence.backgroundAlpha 
     view.addSubview(baseView) 

     contentView.backgroundColor = UIColor.white 
     contentView.layer.cornerRadius = 0.5 
     contentView.frame = CGRect(x: 10, y: 10, width: 200, height: 200) 
     contentView.layer.masksToBounds = true 
     contentView.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY) 


     baseView.addSubview(contentView) 

     headView.frame = CGRect(x: 0, y: 0, width: contentView.bounds.width, height: contentView.bounds.height/2) 
     headView.backgroundColor = UIColor.yellow 
     contentView.addSubview(headView) 



     headImageView.image = UIImage(named: "patternsPlaceholder") 
     headImageView.contentMode = .center 
     headImageView.frame.size = headImageView.image!.size 
     headImageView.center = CGPoint(x: headView.bounds.midX, y: headView.bounds.midY) 
     headView.addSubview(headImageView) 

    } 

    func setVisible(){ 
     let rootView = UIApplication.shared.keyWindow! as UIWindow 
     rootView.addSubview(self.view) 

    } 

    func animateView(view: UIView){ 
     view.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.minY - view.bounds.midY) 
     UIView.animate(withDuration: 0.2, animations: { 
      view.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY + 50) 
     }, completion: {(finished) -> Void in 
      UIView.animate(withDuration: 0.1, animations: { 
       view.center = CGPoint(x: self.view.bounds.midX , y: self.view.bounds.midY) 

      }) 


     }) 
    } 



    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 

そして、以下は私のbaseviewControllerコードです。

import UIKit 

class ViewController: UIViewController { 

    var popView = PopView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func popViewButtonAction(_ sender: Any) { 

//  let popView = PopView() 
     popView.setVisible() 
     popView.animateView(view: popView.contentView) 

     let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.hide(_:))) 
     recognizer.numberOfTapsRequired = 1 
     popView.view.addGestureRecognizer(recognizer) 
     print(String(describing: popView.view.gestureRecognizers)) 

    } 

    func hide(_ recognizer: UITapGestureRecognizer){ 
     self.popView.view.removeFromSuperview() 

     let rootView = UIApplication.shared.keyWindow! as UIWindow 
     rootView.addSubview(self.view) 


    } 



} 

私はあなたにアイデアを与えることを願っています。それが動作しない理由を

おかげで、 シャンカール

+0

thanku !!!もう一つは、uが私はpopview内のコードを置けば – InvisibleMike

+0

あなたは、私は非常に小さな領域を推測しているコンテンツビュー枠を設定している知っています、画面全体でタップ可能な領域を見つけるのが難しいです。コンテンツビューのサイズを大きくすることも、解決策を何でもすることもできます。 –

関連する問題