swift - fade out date picker after 3 seconds without reaction -


i have swift ios8 code, fades date picker in.

  uiview.animatewithduration(0.5, delay: 0.0, options: uiviewanimationoptions.curveeasein, animations: {             self.pickerview.alpha = 1.0             }, completion: nil) 

i want fade automaticaly out, if after 3 seconds picker view has not changed. possible?

i trie this:

        // fade in         uiview.animatewithduration(0.5, delay: 0.0, options: uiviewanimationoptions.curveeasein, animations: {             self.pickerview.alpha = 1.0             }, completion: { finished in                 sleep(3)                 uiview.animatewithduration(0.5, delay: 0.0, options: uiviewanimationoptions.curveeasein, animations: {                     self.pickerview.alpha = 0.0                     }, completion: nil)         }) 

problem is: cant not change value of picker while sleep active.

you need setup timer can used triggering when picker faded out of view. invalidate timer if/when picker value changes:

var timer: nstimer?  override func viewdidload() {     super.viewdidload()      // fade picker in     uiview.animatewithduration(0.5, delay: 0.0, options: uiviewanimationoptions.curveeasein, animations: { () -> void in         self.pickerview.alpha = 1.0     }) { (finished) -> void in          // start timer after fade-in has finished         self.starttimer()      } }  func starttimer() {     self.timer = nstimer.scheduledtimerwithtimeinterval(3.0, target: self, selector: "fadeoutpicker", userinfo: nil, repeats: false) }  func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {      // invalidate timer when picker value changes     timer?.invalidate()      // (re)start timer     starttimer() }  func fadeoutpicker() {     // fade picker out     uiview.animatewithduration(0.5, delay: 0.0, options: uiviewanimationoptions.curveeasein, animations: {         self.pickerview.alpha = 0.0     }, completion: nil) } 

if pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) not being called need become delegate of uipickerview.

as side note, convention variables should not stat uppercase (i.e. self.pickerview should self.pickerview).


Comments