ios - Transition UIButton image from one image to another -


i'm new ios development , using swift language of choice. i'm developing simple little program son displays images of animals on uibuttons , when picks right 1 buttons load new images , he's asked find different animal.

the basics of program , running , can play want on making fancier. when correct image selected , it's time replace button images new ones use:

btnitem.setimage(uiimage(named: arritems[intitemcounter]), forstate: .normal) 

rather changing button's image 1 want change animated transition can't figure out how.

i've found plenty transitions on uiviews , plenty permanently animating button images nothing transitions such dissolve or swipe left right or whatever.

this question on pretty same mine answer uses objective-c.

so, long story short, there way of transitioning uibutton image 1 in swift?

thanks time in advance.

uibutton subclass of uiview can use uiview.animatewithduration button. example can like:

@ibaction func buttonpressed(sender: uibutton) {         uiview.transitionwithview(sender, duration: 1.5, options: .transitionflipfromright, animations: {                 sender.setimage(uiimage(named: arritems[intitemcounter]), forstate: .normal)             }, completion: nil)      } 

which cause, on tapping, button flip new image.

if want animation other example (fade out/in) can say:

@ibaction func buttonpressed(sender: uibutton) {             uiview.animatewithduration(0.5, animations: {                     sender.alpha = 0.0                 }, completion:{(finished) in                     sender.setimage(uiimage(named: arritems[intitemcounter]), forstate: .normal)                     uiview.animatewithduration(0.5,animations:{                     sender.alpha = 1.0                     },completion:nil)              })          } 

you can cross dissolve so:

@ibaction func buttonpressed(sender: uibutton) {         uiview.transitionwithview(sender, duration: 1.5, options: .transitioncrossdissolve, animations: {             sender.setimage(uiimage(named: arritems[intitemcounter]), forstate: .normal)             }, completion: nil)      } 

Comments