1
sender.setBackgroundImage(imageArray[index], forState: .Normal)

sleep(2)         // wait before you flip back over

sender.setBackgroundImage(cardBack, forState: .Normal)

The sleep seems to stop the image from completing its load. How can I wait for the setBackgroundImage to complete?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
DumbBlonde
  • 58
  • 6
  • 1
    You *never* wait. You use the information *when it's available*. With a "callback" for example - UIAnimation has some interesting ones. – Eric Aya Apr 21 '16 at 16:15
  • why don't use closure? – Muzahid Apr 21 '16 at 16:32
  • But the above code is in a callback. Before the callback was invoked, CardBack was displayed as the BackgroundImage. Should I display a UIAnimation of front and back images? And can you use an animation on a button? – DumbBlonde Apr 21 '16 at 16:33
  • Is the UI responsive while you are sleeping? If not, and your touches lag or are ignored, it is definitely the wrong way of implementing. Take a look at the UIView.animateWithDuration() function. You can flip your card (or in this case, change your image over a specified time) and then reverse the process in the completion block with a closure. – Shen Apr 21 '16 at 17:36

1 Answers1

0
sender.setBackgroundImage(imageArray[index], forState: .Normal)
sender.enabled = false;

let delay_time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC))
dispatch_after(delay_time, dispatch_get_main_queue()) {
    sender.setBackgroundImage(cardBack, forState: .Normal)
    sender.enabled = true;
}

Edit: Disable the button while it is showing the image.

Evdzhan Mustafa
  • 3,645
  • 1
  • 24
  • 40
  • Thanks. This works and I learned about closure. But why is image somewhat transparent (or perhaps its brightness increased)? – DumbBlonde Apr 21 '16 at 17:31