ios - CALayer Subclass Repeating Animation -


i'm attempting create calayer subclass performs animation every x seconds. in example below i'm attempting change background 1 random color when running in playground nothing seems happen

import uikit import xcplayground import quartzcore  let view = uiview(frame: cgrect(x: 0.0, y: 0.0, width: 200, height: 200)) xcpshowview("view", view)  class customlayer: calayer {      var colors = [         uicolor.bluecolor().cgcolor,         uicolor.greencolor().cgcolor,         uicolor.yellowcolor().cgcolor     ]      override init!() {         super.init()          self.backgroundcolor = randomcolor()          let animation = cabasicanimation(keypath: "backgroundcolor")          animation.fromvalue = backgroundcolor         animation.tovalue = randomcolor()         animation.duration = 3.0         animation.repeatcount = float.infinity          addanimation(animation, forkey: "backgroundcolor")      }      required init(coder adecoder: nscoder) {         super.init(coder: adecoder)     }      private func randomcolor() -> cgcolor {         let index = int(arc4random_uniform(uint32(colors.count)))         return colors[index]     } }  let layer = customlayer() layer.frame = view.frame view.layer.addsublayer(layer) 

the parameters of repeating animation setup once, can't change color on each repeat. instead of repeating animation, should implement delegate method, animationdidstop:finished:, , call animation again there new random color. haven't tried in playground, works ok in app. notice have implement init!(layer layer: anyobject!) in addition other init methods had.

import uikit  class customlayer: calayer {      var newcolor: cgcolorref!      var colors = [         uicolor.bluecolor().cgcolor,         uicolor.greencolor().cgcolor,         uicolor.yellowcolor().cgcolor     ]      required init(coder adecoder: nscoder) {         super.init(coder: adecoder)     }      override init!(layer: anyobject!) {         super.init(layer: layer)     }      override init!() {         super.init()         backgroundcolor = randomcolor()         newcolor = randomcolor()         self.animatelayercolors()     }       func animatelayercolors() {         let animation = cabasicanimation(keypath: "backgroundcolor")         animation.fromvalue = backgroundcolor         animation.tovalue = newcolor         animation.duration = 3.0         animation.delegate = self          addanimation(animation, forkey: "backgroundcolor")     }      override func animationdidstop(anim: caanimation!, finished flag: bool) {         backgroundcolor = newcolor         newcolor = randomcolor()         self.animatelayercolors()     }       private func randomcolor() -> cgcolor {         let index = int(arc4random_uniform(uint32(colors.count)))         return colors[index]     } } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -