ios - Animating UILabel element with CoreAnimation in Swift -
i'm learning how utilize coreanimation various ui elements. perform complex, queued animations on ui elements on screen transition next view.
now attempting simple. want transform (scale up) uilabel on button tap.
i have next function:
func animateuielementsout(element : anyobject) { var animation : cabasicanimation = cabasicanimation(keypath: "transform"); var transform : catransform3d = catransform3dmakescale(2, 2, 1); animation.setvalue(nsvalue(catransform3d: transform), forkey: "scaletext"); animation.duration = 2.0; element.layer?.addanimation(animation, forkey: "scaletext"); }
i calling function on button tap this:
animateuielementsout(greetingslabel);
the compiler isn't giving off warnings, animation not working. doing wrong here?
as per @david rönnqvist suggestions working code looks (fromvalue / tovalue added):
func animateuielementsout(element : anyobject) { var animation : cabasicanimation = cabasicanimation(keypath: "transform"); animation.delegate = self; var transform : catransform3d = catransform3dmakescale(2, 2, 1); animation.fromvalue = nsvalue(catransform3d: catransform3didentity); animation.tovalue = nsvalue(catransform3d: transform); animation.duration = 2.0; element.layer?.addanimation(animation, forkey: nil); }
the animation doesn't have neither tovalue
or fromvalue
.
you setting transform "scaletext"
key on animation animation doesn't have corresponding property.
to create animation work should set transform tovalue
of animation. however, there still things missing create whole thing work. model value of layer never changed when animation finished , removed, see old (model) values again.
if interested in making scaling animation, recommend utilize higher level uiview animation apis instead:
uiview.animatewithduration(2.0) { element.transform = cgaffinetransformmakescale(2, 2) }
if on other hand interested in working core animation, there couple more things create work.
configure animation go from current model value (usingfromvalue
property) configure animation go to new value (as explained above) update model value new value before adding animation. as side trivia, reason set value "scaletext" without there beingness such property layers , animations work bit differently classes key-value coding. can set , value on both layer , view using key-value coding. means valid code:
let layer = calayer() layer.setvalue(1, forkey: "foo") var foo = layer.valueforkey("foo") // equal 1 allow anim = cabasicanimation(keypath: "bounds") anim.setvalue(2, forkey: "bar") var bar = anim.valueforkey("bar") // equal 2
however, if seek , same on other object, runtime exception saying class isn't key-value coding compliant key. example, code:
let view = uiview(frame: cgrectzero) view.setvalue(3, forkey: "baz")
produces runtime exception:
terminating app due uncaught exception 'nsunknownkeyexception', reason: '[<uiview 0x10dc08b30> setvalue:forundefinedkey:]: class not key value coding-compliant key baz.'
ios core-animation swift
No comments:
Post a Comment