Swift: NSTimer, delay hiding UIButton with each tap -
i have button (actually 2 identical buttons) unhide action in uiview.
the buttons hide automatically 2 seconds after panning in second uiview ends using dispatch_after keep buttons visible if either tapped while visible. here timer property , 2 methods uibutton subclass. have @ibaction calls "justtapped" in viewcontroller.
var timer = nstimer() func hideandshrink(){ if !self.hidden && !timer.valid{ self.hidden = true } } func justtapped(){ timer.invalidate() timer = nstimer.scheduledtimerwithtimeinterval(2.0, target: self, selector: "hideandshrink", userinfo: nil, repeats: false) }
once either button tapped, not hide.
is timer still valid while calls method in selector?
the answer this, luk2302, yes, timer valid when sends selector
how can around this?
as suggested, wrote second method timer hid buttons without !timer.valid ?
it turned out did not need second method @ had attempt hide buttons call justtapped instead. here final code expand , shrink animations both buttons. self.direction direction want button expand, bottom up, or top down, , determines background image. think have been enumeration haven't figured out yet.
func unhideandexpand(){ if self.hidden{ expand(self.frame.size.height) } } func hideandshrink(){ if !self.hidden && !shrinking{ shrink(self.frame.size.height) } } func justtapped(){ timer.invalidate() timer = nstimer.scheduledtimerwithtimeinterval(3.0, target: self, selector: "hideandshrink", userinfo: nil, repeats: false) } func expand(height: cgfloat){ if self.direction == "up"{ self.transform = cgaffinetransformmake(1, 0, 0, 1/height, 1, height/2) } else if self.direction == "down" { self.transform = cgaffinetransformmake(1, 0, 0, 1/height, 1, -height/2) } self.hidden = false uiview.animatewithduration(0.5, delay: 0, options: .curveeaseout, animations: {self.transform = cgaffinetransformidentity}, completion: nil) } func shrink(height: cgfloat){ self.shrinking = true uiview.animatewithduration(0.5, delay: 0, options: .curveeaseout, animations: {if self.direction == "up"{ self.transform = cgaffinetransformmake(1, 0, 0, 1/height, 1, height/2) } else if self.direction == "down" { self.transform = cgaffinetransformmake(1, 0, 0, 1/height, 1, -height/2) } }, completion: {_ in self.transform = cgaffinetransformidentity self.hidden = true self.shrinking = false}) }
thanks help.
is timer still valid while calls method in selector?
yes, is
the following complete runnable playground example demonstrating that:
import foundation import xcplayground xcpsetexecutionshouldcontinueindefinitely() class bla { var timer : nstimer? init() { timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: "check", userinfo: nil, repeats: false) nstimer.scheduledtimerwithtimeinterval(1.1, target: self, selector: "checkagain", userinfo: nil, repeats: false) } @objc func check(){ print(timer!.valid) } @objc func checkagain(){ check() } } let bla = bla()
this first output true
, output false
. timer still valid when firing. leave fired method timer gets invalidated.
that render if
useless since method nothing when fired timer because anything && !true
evaluates false
.
what can circumvent issue create different method timerfired
sets internal property of class, e.g. var timerhasfired = false
true
. `` has check variable instead of timer:
var timer = nstimer() var timerhasfired = false func hideandshrink(){ if !self.hidden && hideandshrink { self.hidden = true } } func timerfired() { timerhasfired = true hideandshrink() } func justtapped(){ timer.invalidate() timer = nstimer.scheduledtimerwithtimeinterval(2.0, target: self, selector: "timerfired", userinfo: nil, repeats: false) }
Comments
Post a Comment