cabasicanimation - Swift: How to draw a circle step by step? -


i build project draw arc initially, arc 1/8 of circle.
then put button on viewcontroller, whenever click button, draw 1/8 of circle seamless on .
got problem: when click button, draws arc rapidly(0.25s), not duration set before(1s).
how reach no matter when click button, consumes same time duration set before?

import uikit  let π = cgfloat(m_pi)  class viewcontroller: uiviewcontroller {  var i: cgfloat = 1 var maxstep: cgfloat = 8  var circlelayer: cashapelayer?  override func viewdidload() {     super.viewdidload()     let startangle = cgfloat(0)     let endangle = 2*π     let ovalrect = cgrectmake(100, 100, 100, 100)      let ovalpath = uibezierpath(arccenter: cgpointmake(cgrectgetmidx(ovalrect), cgrectgetmidy(ovalrect)), radius: cgrectgetwidth(ovalrect), startangle: startangle, endangle: endangle, clockwise: true)      let circlelayer = cashapelayer()     circlelayer.path = ovalpath.cgpath     circlelayer.strokecolor = uicolor.bluecolor().cgcolor     circlelayer.fillcolor = uicolor.clearcolor().cgcolor     circlelayer.linewidth = 10.0     circlelayer.linecap = kcalinecapround     self.circlelayer = circlelayer      self.view.layer.addsublayer(self.circlelayer)      let anim = cabasicanimation(keypath: "strokeend")     // here set duration     anim.duration = 1.0     anim.fromvalue = 0.0     anim.tovalue = self.i/self.maxstep      self.circlelayer!.strokestart = 0.0     self.circlelayer!.strokeend = self.i/self.maxstep      self.circlelayer!.addanimation(anim, forkey: "arc animation")     self.i++ }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  // when action triggered, draws arc rapidly, why? @ibaction func buttonanimate(sender: uibutton) {     if self.i<=self.maxstep {         self.circlelayer!.strokestart = 0.0         self.circlelayer!.strokeend = self.i/self.maxstep         self.i++     } } } 

here correct solution:

import uikit  import uikit  let π = cgfloat(m_pi)  class viewcontroller: uiviewcontroller {      var i: cgfloat = 1     var maxstep: cgfloat = 8      var prevvalue : cgfloat = 0.0      var circlelayer: cashapelayer?      override func viewdidload() {         super.viewdidload()         let startangle = cgfloat(0)         let endangle = 2*π         let ovalrect = cgrectmake(100, 100, 100, 100)          let ovalpath = uibezierpath(arccenter: cgpointmake(cgrectgetmidx(ovalrect), cgrectgetmidy(ovalrect)), radius: cgrectgetwidth(ovalrect), startangle: startangle, endangle: endangle, clockwise: true)          let circlelayer = cashapelayer()         circlelayer.path = ovalpath.cgpath         circlelayer.strokecolor = uicolor.bluecolor().cgcolor         circlelayer.fillcolor = nil //uicolor.clearcolor().cgcolor         circlelayer.linewidth = 10.0         circlelayer.linecap = kcalinecapround         self.circlelayer = circlelayer          self.view.layer.addsublayer(self.circlelayer!)            self.circlelayer!.strokestart = 0.0 //initial stroke-         setstrokeendforlayer(self.circlelayer!, from: 0.0,  to: self.i / self.maxstep, animated: true)          self.i++       }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }       @ibaction func buttonanimate(sender: uibutton) {         if self.i <= self.maxstep {              setstrokeendforlayer(self.circlelayer!, from: (self.i - 1 ) / self.maxstep,  to: self.i / self.maxstep, animated: true)             self.i++         }     }      func setstrokeendforlayer(layer: calayer,  var from:cgfloat, to: cgfloat, animated: bool)     {          self.circlelayer!.strokeend =          if animated         {              //check if there existing animation in progress, if override, value             if let circlepresentationlayer = self.circlelayer!.presentationlayer()             {                 = circlepresentationlayer.strokeend             }              //remove on going animation             if (self.circlelayer?.animationforkey("arc animation") as? cabasicanimation != nil)             {                 //remove current animation                 self.circlelayer!.removeanimationforkey("arc animation")             }               let anim = cabasicanimation(keypath: "strokeend")             // here set duration             anim.duration = 1             anim.fromvalue =             anim.tovalue =               self.circlelayer!.addanimation(anim, forkey: "arc animation")         }     }  } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -