ios - 'Press and hold' animation without NSTimer -
update: nstimer approach works now, comes huge performance hit. question narrowed down approach without nstimers.
i'm trying animate 'press , hold' interactive animation. after following load of answers, i've followed approach in controlling animation timing @david-rönnqvist. , works, if use slider pass layer.timeoffset
.
however, can't seem find way continuously update same animation on press , hold gesture. animation either doesn't start, shows beginning frame or @ points finishes , refuses start again.
can achieving following effect, without horrible nstimer approach i'm experimenting with?
- on user press, animation starts, circle fills up.
- while user holds (not moving finger), animation should continue until end , stay on frame.
- when user lifts finger, animation should reverse, circle empties again.
- if user lifts finger during animation or presses down again during reverse, animation should respond accordingly , either fill or empty current frame.
here's github repo current efforts.
as mentioned, following code works well. it's triggered slider , job great.
func animationtimeoffsettopercentage(percentage: double) { if fillshapelayer == nil { fillshapelayer = constructfillshapelayer() } guard let fillanimationlayer = fillshapelayer, let _ = fillanimationlayer.animationforkey("animation") else { print("animation not found") return } let timeoffset = maximumduration * percentage print("set animation percentage \(percentage) timeoffset: \(timeoffset)") fillanimationlayer.timeoffset = timeoffset }
however, following approach nstimers works, has incredible performance hit. i'm looking approach doesn't use nstimer.
func beginanimation() { if fillshapelayer == nil { fillshapelayer = constructfillshapelayer() } animationtimer?.invalidate() animationtimer = nstimer.schedule(interval: 0.1, repeats: true, block: { [unowned self] () -> void in if self.layer.timeoffset >= 1.0 { self.layer.timeoffset = self.maximumduration } else { self.layer.timeoffset += 0.1 } }) } func reverseanimation() { guard let fillanimationlayer = fillshapelayer, let _ = fillanimationlayer.animationforkey("animation") else { print("animation not found") return } animationtimer?.invalidate() animationtimer = nstimer.schedule(interval: 0.1, repeats: true, block: { [unowned self] () -> void in if self.layer.timeoffset <= 0.0 { self.layer.timeoffset = 0.0 } else { self.layer.timeoffset -= 0.1 } }) }
when use slider use fillanimationlayer
layer animation
fillanimationlayer.timeoffset = timeoffset
however, in beginanimation
, reverseanimation
functions using self.layer
.
try replace self.layer.timeoffset
self.fillshapelayer!.timeoffset
in timer blocks.
Comments
Post a Comment