Thanks for help. This is the solution:
I created the subview and i add a gesture to remove it
@IBAction func infoView(sender: UIButton) {
var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
testView.backgroundColor = UIColor.blueColor()
testView.alpha = 0.5
testView.tag = 100
testView.userInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = "removeSubview"
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
println("Start remove sibview")
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}else{
println("No!")
}
}
Update:
Swift 3+
@IBAction func infoView(sender: UIButton) {
let testView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
testView.backgroundColor = .blue
testView.alpha = 0.5
testView.tag = 100
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(GasMapViewController.removeSubview)
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
print("Start remove sibview")
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}else{
print("No!")
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…