Swift addsubview并将其删除

2020/11/22 08:51 · ios ·  · 0评论

我想添加子视图并一键删除。这是我的代码:

/ *添加子视图* /

var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
testView.backgroundColor = UIColor.blueColor()
testView.alpha = 0.5
testView.tag = 100
super.view.userInteractionEnabled = false
self.view.userInteractionEnabled = true
self.view.addSubview(testView)

/ *删除子视图* /

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject() as UITouch
    let point = touch.locationInView(self.view)

    if(testView.tag==100){
        println("Tag 100")
        testView.removeFromSuperview()
    }
    else{
        println("tag not found")
    }

}

但是删除它不起作用有人可以帮助我吗?谢谢!

感谢帮助。这是解决方案:我创建了子视图,并添加了将其删除的手势

@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!")
    }
}

更新:

迅捷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!")
    }
}

您必须使用该viewWithTag函数查找具有给定的视图tag

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject() as UITouch
    let point = touch.locationInView(self.view)

    if let viewWithTag = self.view.viewWithTag(100) {
        print("Tag 100")
        viewWithTag.removeFromSuperview()
    } else {
        print("tag not found")
    }
}

假设您可以通过销售点或编程代码访问它,则可以通过引用您的视图fooremoveFromSuperview方法将其删除

foo.removeFromSuperview()

我在自定义CollectionViewCell中有一个视图,并在该视图上嵌入了一个图形。为了刷新它,我必须检查该视图上是否已经放置了图形,将其删除,然后应用新图形。这是解决方案

cell.cellView.addSubview(graph)
graph.tag = 10

现在,在您要删除它的代码块中(在您的情况下,gestureRecognizerFunction)

if let removable = cell.cellView.viewWithTag(10){
   removable.removeFromSuperview()
}

再次嵌入

cell.cellView.addSubview(graph)
graph.tag = 10

使用XCode 8和Swift 3测试了此代码

要将自定义视图添加到SuperView,请使用:

self.view.addSubview(myView)

要从Superview中删除自定义视图,请使用:

self.view.willRemoveSubview(myView)

本文地址:http://ios.askforanswer.com/swift-addsubviewbingjiangqishanchu.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!