2016-12-05 56 views
0

我有一個覆蓋與表中,我想添加一個觸摸手勢識別器到背景來消除視圖,並addTarget添加到覆蓋中的按鈕做同樣的事情。addTarget和addGestureRecognizer不工作,沒有崩潰/錯誤

疊加顯示正常,但是每當我點擊黑色背景或取消按鈕時,什麼都不會發生。我在這裏搜索了一個答案,但沒有發現任何工作。我的代碼如下,隨後覆蓋的截圖:

class importedFileView: NSObject { 

let blackView = UIView() 

let importedFileContainerView: UIView = { 
    let importedFileContainerView = UIView(frame: .zero) 
    importedFileContainerView.backgroundColor = .white 
    importedFileContainerView.layer.cornerRadius = 10 
    importedFileContainerView.layer.masksToBounds = true 
    return importedFileContainerView 
}() 

let headerLabel: UILabel = { 
    let headerLabel = UILabel() 
    headerLabel.translatesAutoresizingMaskIntoConstraints = false 
    headerLabel.font = UIFont(name: "HelveticaNeue-Thin" , size: 24) 
    headerLabel.text = "Attach file" 
    headerLabel.textColor = .darkGray 
    headerLabel.adjustsFontSizeToFitWidth = true 
    return headerLabel 
}() 

let fileTableView: UITableView = { 
    let fileTableView = UITableView() 
    return fileTableView 
}() 


let updateDetailsButton: UIButton = { 
    let updateDetailsButton = UIButton() 
    updateDetailsButton.translatesAutoresizingMaskIntoConstraints = false 
    updateDetailsButton.backgroundColor = UIColor(r:40, g:86, b:131) 
    updateDetailsButton.setTitleColor(UIColor.white, for: .normal) 
    updateDetailsButton.setTitle("Attach selected files", for: .normal) 
    updateDetailsButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light" , size: 18) 
    updateDetailsButton.layer.cornerRadius = 2 
    return updateDetailsButton 
}() 

let cancelButton: UIButton = { 
    let cancelButton = UIButton() 
    cancelButton.translatesAutoresizingMaskIntoConstraints = false 
    cancelButton.backgroundColor = UIColor.white 
    cancelButton.setTitleColor(UIColor(r:40, g:86, b:131), for: .normal) 
    cancelButton.setTitle("Cancel", for: .normal) 
    cancelButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light" , size: 18) 
    cancelButton.layer.cornerRadius = 2 
    return cancelButton 
}() 

let frameHeight: CGFloat = 450 

func showFormView(){ 

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


     window.addSubview(blackView) 
     window.addSubview(importedFileContainerView) 
     importedFileContainerView.addSubview(headerLabel) 
     importedFileContainerView.addSubview(fileTableView) 
     importedFileContainerView.addSubview(updateDetailsButton) 
     importedFileContainerView.addSubview(cancelButton) 

     cancelButton.addTarget(self, action: #selector(handleDismiss), for: .touchUpInside) 
     blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss))) 

     layoutViews() 

     fileTableView.frame = CGRect(x: 30, y: window.frame.height, width: window.frame.width - 60, height: 230) 
     let frameY = (window.frame.height - frameHeight)/2 

     importedFileContainerView.frame = CGRect(x: 20, y: window.frame.height, width: window.frame.width - 40, height: self.frameHeight) 

     blackView.frame = window.frame 
     blackView.alpha = 0 

     UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.blackView.alpha = 1 
      self.importedFileContainerView.frame = CGRect(x: 20, y: frameY, width: window.frame.width - 40, height: self.frameHeight) 
     }, completion: nil 
     ) 
    } 
} 


func layoutViews(){ 
    let views = ["v0" : headerLabel, "v1": fileTableView, "v2": updateDetailsButton, "v3": cancelButton] 

    let leftSpace = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v0]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views) 
    let leftSpace1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v1]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views) 
    let leftSpace2 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v2]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views) 
    let leftSpace3 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v3]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views) 

    let topSpacing = NSLayoutConstraint.constraints(withVisualFormat: "V:|-20.0-[v0(40)]-20.0-[v1(230)]-20.0-[v2(50)]-10.0-[v3(50)]-10.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views) 

    importedFileContainerView.addConstraints(topSpacing) 
    importedFileContainerView.addConstraints(leftSpace) 
    importedFileContainerView.addConstraints(leftSpace1) 
    importedFileContainerView.addConstraints(leftSpace2) 
    importedFileContainerView.addConstraints(leftSpace3) 

} 

func handleDismiss() { 

    UIView.animate(withDuration: 0.5, 
        delay: 0.0, 
        options: .curveEaseInOut, 
        animations: { 
        self.blackView.alpha = 0 

        if let window = UIApplication.shared.keyWindow { 
         self.importedFileContainerView.frame = CGRect(x: 20, y: window.frame.height, width: window.frame.width - 40, height: self.frameHeight) 

        } 
    }, 
        completion: { [weak self] finished in 
        self?.blackView.removeFromSuperview() 
        self?.importedFileContainerView.removeFromSuperview() 
    }) 

} 

override init() { 
    super.init() 
} 
} 

enter image description here

回答

0

當您的疊加層可見時,您是否保留對importedFileView實例的強烈參考?就我所測試的情況而言,當目標丟失時,所有的動作都會被忽略。

例如,這不起作用:

@IBAction func someAction(_ sender: Any) { 
    let ifv = importedFileView() 
    ifv.showFormView() 

    //`ifv` is released at the end of this method, then your overlays are shown... 
} 

這工作:

let ifv = importedFileView() //keep the instance as a property of your ViewController. 
@IBAction func someAction(_ sender: Any) { 
    ifv.showFormView() 
} 

程序生成UIView小號isUserInteractionEnabled默認爲true。您無需明確將其設置爲true。順便說一句,你最好不要將你的非UIView類命名爲...View,並且最好讓你的類型名字以大寫字母開頭,這樣你的代碼對於有經驗的Swift程序員來說更具可讀性。

+0

賓果!謝謝。將重命名類膨脹,謝謝。 –

2

self.blackView.isUserInteractionEnabled = true;

是所有你需要添加到blackViewUIView)。

沒有這一點,視圖不會啓用任何交互,因此手勢識別器的目標/操作不會被觸發。

事件被忽略。

https://developer.apple.com/reference/uikit/uiview/1622577-isuserinteractionenabled

您可能還需要動畫過程中禁用它。

+0

我嘗試過,但沒有效果,所以再次刪除它。我會把它扔回去,看看我能看到什麼東西 –

+0

加回來,仍然不工作。還嘗試將addTarget和GestureRecognizer移動到動畫的完成塊,以確保它們不被禁用並且不能重新啓用。還要在完成時打印出一行,以確保動畫報告已完成。 –