Bryan ORTIZ
Bryan ORTIZ

Reputation: 316

Animation on image inside UIButton in swift

I made my own toolBar with a main action button on the middle of the bar.

When I click on a different button, my main action button image changes. I'd like to make it "obvious" by making an animation (the best would be something like UIImageView startAnimating()) but I fail at it.

Is there anything like the startAnimating() for the image inside UIButton to show the translation from an image to another ?

I dont wanna use sleep(1) or usleep(200) to block my app.

Any help or advice or suggestion would be appreciated.

Upvotes: 1

Views: 671

Answers (1)

LuKenneth
LuKenneth

Reputation: 2237

What I have done in the past is flash a color on the view to give feedback to the user that what they have done was registered with the app. In your case you could flash a color or something in between the change in images. I did this using a delay method I wrote which you can use by switching the color of the button, then delaying for about half a second or so, then show the next image. This will make it obvious that a change has been made. Here's the method to delay:

 func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
    dispatch_after(time, dispatch_get_main_queue(), block)
}

You can put your image swap inside the block parameter. Hope this helps

Upvotes: 1

Related Questions