ios - Start/stop image view rotation animations -


sorry if newbie question, new ios & swift.

i have start/stop button , image view want rotate.

when press button want start rotating , when press button again image should stop rotating. using uiview animation, haven't figured out way stop view animations.

i want image rotate, when animation stops image shouldn't go starting postion, instead continue animation.

var istapped = true      @ibaction func startstopbuttontapped(_ sender: any) {        ruotate()        istapped = !istapped     }      func ruotate() {         if istapped {             uiview.animate(withduration: 5, delay: 0, options: .repeat, animations: { () -> void in             self.imagewood.transform =     self.imagewood.transform.rotated(by: cgfloat(m_pi_2))             }, completion: { finished in             ruotate()             })     }  } 

it code, doesn't work aspect.

swift 3.x

start animation

let rotationanimation : cabasicanimation = cabasicanimation(keypath: "transform.rotation.z")     rotationanimation.tovalue = nsnumber(value: .pi * 2.0)     rotationanimation.duration = 0.5;     rotationanimation.iscumulative = true;     rotationanimation.repeatcount = .infinity;     self.imagewood?.layer.add(rotationanimation, forkey: "rotationanimation") 

stop animation

self.imagewood?.layer.removeanimation(forkey: "rotationanimation") 

swift 2.x

start animation

let rotationanimation : cabasicanimation = cabasicanimation(keypath: "transform.rotation.z")     rotationanimation.tovalue = nsnumber(double: m_pi * 2.0)     rotationanimation.duration = 1;     rotationanimation.cumulative = true;     rotationanimation.repeatcount = .infinity;     self.imagewood?.layer.addanimation(rotationanimation, forkey: "rotationanimation") 

stop animation

self.imagewood?.layer.removeanimation(forkey: "rotationanimation") 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -