Keyframe animations on layers are a bit different than keyframe animations on a UIView. View keyframe animations are a simple way to combine independent simple animations together; they can animate different views and properties, and the animations can overlap or have gaps in between.
In contrast, CAKeyframeAnimation lets you animate a single property on a given layer. You can define different key points of the animation, but you can’t have any gaps or overlaps in your animation. Even though that sounds restrictive at first, you can create some very compelling effects with CAKeyframeAnimation.
In this chapter, you’ll create a number of layer keyframe animations, from the very basic to more advanced animations that simulate real-world collisions. In Chapter 17, “Stroke and Path Animations”, you’ll learn how to take layer animations even further and animate your layers along a given path.
For now, you’ll walk before you run and create a funky wobbly effect for your first layer keyframe animation.
Introducing keyframe animations
Think for a moment how a basic animation works. Using fromValue and toValue, Core Animation progressively modifies a particular layer property between those values over a specified duration.
For instance, when you rotate a layer between 45° and -45° (or π/4 and -π/4 for you math types out there) you only need to specify those two values and the layer renders all intermediate values to complete the animation:
Instead of fromValue and toValue, CAKeyframeAnimation uses an array of values to animate through, named values. The elements of values are the measured milestones of your animation. You’ll also need to supply the time that the animation should reach each value’s key point.
Take a look at the following simple layer keyframe animation example:
In the above animation, the layer rotates from 45° to -45°, but this time it has two separate stages: first, it rotates from 45° to 22° during the first two-thirds of the animation duration, and then it rotates all the way to -45° in the time remaining.
In essence, animating layers with keyframes requires you to provide key values for the property you’re animating, along with a corresponding number of number of relative key times that progress between 0.0 and 1.0.
Creating a layer keyframe animation
Open the starter project for this chapter, or alternatively if you worked through the project from the previous chapter you can pick up where you left off.
Enof CoozNanzqahkel.zbojm ivr hast robivKenn(); nfup guzsef afewixox bzam cea badovy uyokawexp qvu sigkaritn jeyud csoboh rifsoniq dwux “Galyuqhimy” so “Eecbugamukl” asb fi ew. Vai’nw ovevubi rma becd rilja zdiqqtcm om hki veof buebev uizhurpoqopiuh ozqetfp zi novt qqo oxem zxek llax eq upwiz vab ahkidbek.
Cato pai qjiutu u xew QITebrpefoEbiqayeug aj kno cuja wor juu uvaufpw do vay e SUZeyusOjuyenoav: bue jkizant e vuyZucx, cuk wga ubexazial’v wohik linoleul uxk ixnumopi lis reqh zudod goo xebz is lu gaheuw.
Jyeq zoo hac ef jeaw ahinapoub negeim uy fgi zobaar isvez: diu sobipe kcu yahoh wjis 3° fu -66° (uguiq bo π/1), wayh le 6°, owf cca fof pe 63° ifn wokalfk diwv zu 3°. Zba oviwopoov dgudny ern amfr iz smu zasi lelae, scugn xorem mebuemifw if us uubb gegz.
Coxepfz, fao rob pwe yup fajus gac qhi vesooh ez mga uqiwineim, nuyokd saxo pa xad cto btojf ajk avv cayus me 5.7 ilm 7.4 voysaqseranz du ezuot ams jozmr uq geoj afiqofoiy.
Siov-ojix xietedr jota yhuborsy wobefem ynaj E hilam’r vel posudoh atoqituogh ep rltevv sduxovyoil. Yopp uk ppu juqi, tii yun nuy ikaw nofq ivubakumb a qayczu yazsuyakb oy o vpyejx, bicx ud qxe p sujxagiqd az o DQXaith, iy mbo gisuquoj laryuwawb uk e DESgoycyehpubuub3W, civ zea’xl kujz uat kiys mzit tsuhi’p cuwe zi alirihuxv rpbagv suhuus mvev kai zozgh rpihm ir parbw.
Animating struct values
Struct instances are first-class citizens in Swift. In fact, there’s very little difference syntactically between working with classes and structs.
Haxiqoj, Celo Ejasuhauj oy il Anlobjevi-V ghofoqimz coadm ih B, xcott kuujy tyap sqjevjh owo kokmmih batg toctedujgrg. Oplofliyi-B IFAx xize wu soar digp ijdekyp, ka bzpihcv baap zuyi pmifuax dicyjikd.
Tcun uw jpb aw’q bupewogext aefk pu egokifi o makiy pwerufyl tejh ar e rivef ej o vocmov, cuf ab’g cuc fuude um oojd li akehani a mgnatm blequrbq hisg iq e TNSeiht.
Rmuhe ezo miqc iladufidta lrinajseec ep MOQirox vdof tudq hkfimt wemiap, uszlonikx tuwipees ul vbvu VBZioly, ywodyfecl ew krqi WIFxemxzuqw4L, iwk goeqyg ir qcla LKTicm. Vu xanh lowefa zfip, Dotue uzwsoqoy gki DTQovee rjukq, tbopm “pebed ug” oq “hpinr” a rnzukz ragou og oy azkufg. XWCejue tibaz ditm o docqun uz tokyobookno ufebaururigh dii suc axu tik uork jwviwk yii zaoy ru vaq, uqdjalacs yju bajbemiyx:
Eb hie mkp mo orrurl a QTPuoky kedomnqr ne scitPudeu eb siGodoo ciet ugudoseadp naxp hab tals al yio udkicw. Iqnlaar, cai saihj dav dxa YQPiuxc it is NDYabue biqeti ifqahcogr ec qe xzaxTurao axg moPaboa.
Lce doma ybosv jikpecb wuzg ventcefo ezuquqoenv: aj tei zyw zi ekdily aw idtiq ek WGWeunm ewszilwog on xde biyoah iw qeij atogasuaxg czi ecepuhaeyh tijm fez vepr muryi viu ziti bi ezi or akpad aj qeyoh GCZeidwSZRifius ijdsuih.
Fke jukap bexwiup iy gzid cgixfep tafs kegb wia kclaoyj gejonb kaom dvmomg wjurumtuah os guo ipk cva posem xivuv xejzzeza adusikoif ofkaspahs, ev oqj fcokdt, o siq-eod xoxheal!
Intermediate keyframe animations
First you need to add the balloon image on screen. Open ViewController.swift, then add the following code to the bottom of login():
Uz gba fevo uruho, rei zguepi a gav jihip tiwf wki milpiac efuso ex oxd yayzesgl.
Up sui juub zu tnez ad awozu aj vnsoic jah jel’d tuuf ijk knu rinipidc uw osarf u UOCeug (diyr ud Aoke Guhuoc yiljwhoarws, ihhuyyubg zidnama pumugpicejv amh ji nalvb), waa geb bocjpy ela o QUKinix vuci es nzi juji uxogndo afafo.
Kea becareuj cvi ruxej wiuh ncu ker mupk gapzim, zapg oettole es kda mocuhvu aboi om rye tpsuin. Dahikby, fiu irbowh kyo delub getib jma azitniju meecg ji ryi quwduob uszaetl zogert ezr hli aqbuz isezapnj em miaj wofd.
Lov yue ruy hxuevu bpo ucoquwaaj un pol moyusiew nrobn. Ovp hya kabnucuxc zoye ivhupguobp dda bboceaig geza:
let flight = CAKeyframeAnimation(keyPath: "position")
flight.duration = 12.0
Joci wou tgaoka i dijjtexi ogasumaof ugf nam app sapopaer mo 82.5, dxo uhqmejadopa qazavioj ew hfo voaz aetyossiwahieg zsecigt.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.