Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择子视图时将其删除_Ios_Swift_Swift3 - Fatal编程技术网

Ios 选择子视图时将其删除

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

在这里,我通过单击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 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()
}