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?

  1. on user press, animation starts, circle fills up.
  2. while user holds (not moving finger), animation should continue until end , stay on frame.
  3. when user lifts finger, animation should reverse, circle empties again.
  4. if user lifts finger during animation or presses down again during reverse, animation should respond accordingly , either fill or empty current frame.

the animation want achieve, pressing , holding

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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -