bhzag
bhzag

Reputation: 2980

Appearing and Disappearing UIButton

I'm new to Objective C and swift (I guess we are all new to swift) but I am trying to make a UIButton appear and disappear in different locations on the screen in my app. This is what I've tried in one of my view controllers so far but it doesn't seem to work.

func addButton() {
  var start: CFTimeInterval
  var elapsedTime:CFTimeInterval
  let Button = UIButton()
  let picture = UIImage(named: "picture.png")
  Button.setImage(picture, forState: UIControlState.Normal)
  Button.frame = CGRectMake(0, 142, 106.6, 106.5)
  self.view!.addSubview(Button)
  while (elapsedTime < 1.0) {
      elapsedTime = CACurrentMediaTime() - start
  }
  Button.removeFromSuperView()
}

Upvotes: 0

Views: 1009

Answers (1)

Mundi
Mundi

Reputation: 80273

You could use the convenient GCD API for the timing

dispatch_after(dispatch_time_t(1.0), dispatch_get_main_queue(), {
    button.removeFromSuperView()
})

If it is always the same button, it would be better to create a variable or outlet and just recycle the button (you just let it appear and disappear by setting the alpha or the hidden property. If it is just supposed to be blinking, you could use a basic CAAnimations instead.

NB: Please get into the habit of using variable names that start with small letters, or you will end up mistaking them for class names.

Upvotes: 1

Related Questions