Swift addsubview并将其删除(Swift addsubview and remove it)

编程入门 行业动态 更新时间:2024-10-24 00:19:09
Swift addsubview并将其删除(Swift addsubview and remove it)

我想添加子视图并只需点击一下即可删除。 这是我的代码:

/ *添加子视图* /

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

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

I want to add sub view and remove with one tap. This is my code:

/* To add subview */

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)

/* To remove subview */

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

But the remove it isn't working Someone can help me please? Thanks!

最满意答案

您必须使用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) { println("Tag 100") viewWithTag.removeFromSuperview() } else { println("tag not found") } }

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

更多推荐

本文发布于:2023-08-06 16:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将其   Swift   addsubview   remove

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!