这是一个测试代码,我想将uiview添加到storyBoard,等待1秒钟,然后将其删除。但是uiview没有出现,该代码已关闭

    var uiview1 = UIView()
    uiview1.frame = CGRectMake(0, 0, 50, 50)
    uiview1.backgroundColor = UIColor.blackColor()
    self.view.addSubview(uiview1)
    sleep(1)
    uiview1.removeFromSuperview()

最佳答案

sleep()这样不是一个好主意。它将变得完全无响应且受阻。请改用NSTimer.scheduledTimerWithTimeInterval

class ViewController: UIViewController {
  var uiview1 = UIView()
  override func viewDidLoad() {
    super.viewDidLoad()
    uiview1.frame = CGRectMake(0, 0, 50, 50)
    uiview1.backgroundColor = UIColor.blackColor()
    self.view.addSubview(uiview1)
    var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("dismissView"), userInfo: nil, repeats: false)
  }

  func dismissView() {
    uiview1.removeFromSuperview()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }
}

关于swift - swift UIview removeFormSuperview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29024175/

10-11 14:43