ios - CAKeyFrameAnimation with Color from patternImage -
i trying animate backgroundcolor of calayer cakeyframeanimation. when make classics colors works; color generated patternimage uicolor(patternimage:"imagename").cgcolor
, doesn't work @ all.
@ibaction func animfirstview(sender: anyobject) { addanim(view1, colors: [uicolor.blackcolor().cgcolor, uicolor.redcolor().cgcolor]) // works } @ibaction func animsecondview(sender: anyobject) { var firstcolor = uicolor(patternimage: uiimage(named:"stars1")!).cgcolor; addanim(view2, colors: [firstcolor]); // doesn't works @ :( } func addanim(view:uiview, colors:[cgcolor]) { let anim = cakeyframeanimation(keypath:"backgroundcolor") anim.values = colors; anim.keytimes = [0.0, 0.25, 0.5, 0.75, 1]; anim.calculationmode = kcaanimationdiscrete anim.duration = 0.3; anim.repeatcount = float.infinity; view.layer.addanimation(anim, forkey: "backgroundcolor"); }
someone has idea?
isn't possible or doing wrong?
a test case of "bug" https://github.com/lastmove/patternbugdemo
i don't know why setting backgroundcolor
pattern color part of keyframe animation doesn't work. solved imitating my own existing example set contents
keyframe animation:
@ibaction func animsecondview(sender: anyobject) { var firstcolor = uicolor(patternimage: uiimage(named:"stars1")!) var secondcolor = uicolor(patternimage: uiimage(named:"stars2")!) addanim(view2, colors: [firstcolor, secondcolor, firstcolor, secondcolor]); } func addanim(view:uiview, colors:[uicolor]) { let anim = cakeyframeanimation(keypath:"contents") anim.values = colors.map { col in uigraphicsbeginimagecontextwithoptions(view.bounds.size, true, 0) col.setfill() uibezierpath(rect: view.bounds).fill() let im = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return im.cgimage } anim.keytimes = [0.0, 0.25, 0.75, 1.0]; anim.calculationmode = kcaanimationdiscrete anim.duration = 1; anim.repeatcount = float.infinity; view.layer.addanimation(anim, forkey: nil); }
edit: here's workaround: keep using layer background color, change directly, timer:
@iboutlet weak var view2: uiview! var colors = [uicolor]() var timer : nstimer! var second = false @ibaction func animsecondview(sender: anyobject) { var firstcolor = uicolor(patternimage: uiimage(named:"stars1")!) var secondcolor = uicolor(patternimage: uiimage(named:"stars2")!) self.colors = [firstcolor, secondcolor] if let timer = self.timer { timer.invalidate() } self.timer = nstimer.scheduledtimerwithtimeinterval(0.5, target: self, selector: "anim:", userinfo: nil, repeats: true) } func anim(t:nstimer) { view2.layer.backgroundcolor = self.colors[self.second ? 1 : 0].cgcolor self.second = !self.second }
Comments
Post a Comment