Ios 选择子视图时将其删除
在这里,我通过单击superView将子视图添加到superView:Ios 选择子视图时将其删除,ios,swift,swift3,Ios,Swift,Swift3,在这里,我通过单击superView将子视图添加到superView: @IBAction func tapRecognizerAction(_ sender: UITapGestureRecognizer) { let location = sender.location(in: self.view) addNew(x: location.x, y: location.y) } func addNew(x : CGFloat, y : CGFloat){ let te
@IBAction func tapRecognizerAction(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: self.view)
addNew(x: location.x, y: location.y)
}
func addNew(x : CGFloat, y : CGFloat){
let testView: CustomView = CustomView(frame: CGRect(x: x, y: y, width: 50, height: 50))
testView.backgroundColor = .blue
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(PlanViewController.removeSubview)
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
}
触摸特定子视图时,如何删除它。您需要以这种方式通过点击的视图
func addNew(x : CGFloat, y : CGFloat){
let testView: CustomView = CustomView(frame: CGRect(x: x, y: y, width: 50, height: 50))
testView.backgroundColor = .blue
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(PlanViewController.removeSubview(tapGestureRecognizer:))
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
然后在处理程序中检索点击的视图,然后将其从超级视图中删除
func removeSubview(tapGestureRecognizer: UITapGestureRecognizer){
let tappedView = tapGestureRecognizer.view as! CustomView
tappedView.removeFromSuperview()
}
您需要以这种方式传递已点击的视图
func addNew(x : CGFloat, y : CGFloat){
let testView: CustomView = CustomView(frame: CGRect(x: x, y: y, width: 50, height: 50))
testView.backgroundColor = .blue
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(PlanViewController.removeSubview(tapGestureRecognizer:))
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
然后在处理程序中检索点击的视图,然后将其从超级视图中删除
func removeSubview(tapGestureRecognizer: UITapGestureRecognizer){
let tappedView = tapGestureRecognizer.view as! CustomView
tappedView.removeFromSuperview()
}
在函数外部保留对视图的引用如何
let testView: CustomView = CustomView(frame: CGRect(x: x, y: y, width: 50, height: 50))
func addNew(x : CGFloat, y : CGFloat){
testView.backgroundColor = .blue
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(PlanViewController.removeSubview)
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
testView.removeFromSuperview()
}
在函数外部保留对视图的引用如何
let testView: CustomView = CustomView(frame: CGRect(x: x, y: y, width: 50, height: 50))
func addNew(x : CGFloat, y : CGFloat){
testView.backgroundColor = .blue
testView.isUserInteractionEnabled = true
self.view.addSubview(testView)
let aSelector : Selector = #selector(PlanViewController.removeSubview)
let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)
testView.addGestureRecognizer(tapGesture)
}
func removeSubview(){
testView.removeFromSuperview()
}