2016-11-27 46 views
1

我不明白为什么我的水龙头手势不起作用。黑色取消窗口显示正常,但是当我点击黑色窗口时,都不会调用handleCancel()函数。有任何想法吗?斯威夫特3代表,协议 - TapGesture不工作

import UIKit 

protocol BlackCancelWindowDelegate { 
    func handleCancel() 
} 

class BlackCancelWindow: NSObject { 
    let blackView = UIView() 
    var delegate: BlackCancelWindowDelegate? 
    var frame: CGRect? 

    override init() { 
     super.init() 
    } 

    func showCancelWindow() { 
     if let window = UIApplication.shared.keyWindow { 
      blackView.backgroundColor = UIColor(white: 0, alpha: 0.5) 
      blackView.frame = frame ?? window.frame 
      blackView.alpha = 0 

      window.addSubview(blackView) 
      let tapToDismiss = UITapGestureRecognizer(target: self, action: #selector(handleCancel)) 
      tapToDismiss.numberOfTapsRequired = 1 
      blackView.addGestureRecognizer(tapToDismiss) 


      UIView.animate(withDuration: 0.5,animations: { 
       self.blackView.alpha = 1 
      }, completion: nil) 
     } 
    } 

    func handleCancel() { 
     print("BlackCancelWindow cancel") 
     UIView.animate(withDuration: 0.5, animations: { 
      self.blackView.alpha = 0 
     }, completion: nil) 

     delegate?.handleCancel() 
    } 

} 

class ViewController: UIViewController, BlackCancelWindowDelegate { 

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

    @IBAction func showCancelWindow(_ sender: Any) { 
     let cancelWindow = BlackCancelWindow() 
     cancelWindow.delegate = self 
     cancelWindow.showCancelWindow() 
    } 

    func handleCancel() { 
     print("Delegate cancel") 
    } 
} 

的StackOverflow要我一些更多的细节添加到我的问题,但我不知道把还有什么在这里。

+0

我试着向我的blackView添加一个按钮,该按钮也不会响应点击。出于某种原因,我觉得我的blackView不会读取任何手势。 – jwasher

+0

尝试将'userInteractionEnabled'属性设置为true – KrishnaCA

+0

尝试过。没有骰子。尽管我确实找到了解决方案。将在下面添加。 – jwasher

回答

0

不知道为什么这个工作和我以前的代码没有,但诀窍是子类UIView而不是NSObject。像这样......

import UIKit 

protocol BlackCancelViewDelegate { 
    func handleCancel() 
} 

class BlackCancelView: UIView { 
    var delegate: BlackCancelViewDelegate? 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     backgroundColor = UIColor(white: 0, alpha: 0.5) 
    } 

    convenience init() { 
     if let window = UIApplication.shared.keyWindow { 
      self.init(frame: window.frame) 
     } else { 
      self.init() 
     } 
    } 

    func showCancelWindow() { 
     if let window = UIApplication.shared.keyWindow { 
      alpha = 0 

      window.addSubview(self) 
      let tapToDismiss = UITapGestureRecognizer(target: self, action: #selector(handleCancel)) 
      addGestureRecognizer(tapToDismiss) 

      UIView.animate(withDuration: 0.5,animations: { 
       self.alpha = 1 
      }, completion: nil) 
     } 
    } 

    func handleCancel() { 
     print("BlackCancelView cancel") 

     UIView.animate(withDuration: 0.5,animations: { 
      self.alpha = 0 
     }, completion: nil) 

     delegate?.handleCancel() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

class ViewController: UIViewController, BlackCancelViewDelegate { 

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

    @IBAction func showCancelWindow(_ sender: Any) { 
     let cancel = BlackCancelView() 
     cancel.delegate = self 
     cancel.showCancelWindow() 
    } 

    func handleCancel() { 
     print("Delegate cancel") 
    } 
} 

我不确定我是否以正确的方式完成了这些init函数。请让我知道是否有更好的方法来做到这一点。