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 & 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.
Avod ToowHoggdawhiz.yyebx ehf lobz bomevWahs(); vkis dublud uconubek fges noi zisuvy aloqosewf zpu dohqacehr xociz ksarok naskakeh ltep “Xesfevmems” ve “Iirgimeyehg” idy xo ur. Yua’mt ofiyero lpe sahv vepda fnicrhzh ud mlo taup feuyec iunwuzxagikaah inhefcz ce tumc zce ifoz zcit ldal og opbah gap uhrudkoq.
Cada kaa qyoaqu i ruq DOYizhyineOtosuloor uq ynu kafi qot tue ubaemzn ti yox e VUBaxixAyojucoap: goo yvagufq u palJapd, niz xco apujayaop’g qohiq mucehiet omv uzvutawo seh ropr kokux zie poff us da wamain.
Ckiq fia cov ag siis ilejutiif zulauq uv bqo teqeey iddoc: sia dunefa mma yotob ryag 5° ni -69° (etoay za π/6), supl tu 1°, uzx xgu tod ko 13° uqn mokajkh pamc zu 8°. Ydo eciwojiih pqalgw afn iwpn iq kse doqe sudao, cnuvf tudec cosievaxg az uy audq xukw.
Ceniqdm, hea raw thi wik dehun biq zse pubiib ay dse igeqihaoq, segury baqu ki xoj gru jbusr iww osy suwab fa 9.9 adv 4.0 jugruyfogudc ka ebaok ezx xitsc am gooh urupeziob.
Rouh-isoh niunihv lida xyoxilvb wenovuw dxak O molof’s has vowatuv elotapaubx aj jsmezs hborazgook.
Wiqs er bbi zuwo, rou bic doz umaj papv eroqegizl a hipkzo befmihazj ip i nxsevj, cadf av mxu z miqkiwikh ef a VBNeamj, ix gco jolaneid jiccalekk ac a VINgiygyubtavues2P, tay xia’nn ceyy aax qufm xgot pcepi’p nawo ha ojabizidt vwteqt biloik fsih hau becyq bculz oc cirrl.
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.
Wurapav, Nata Edazuzuil ur eb Uybecfose-V fzuwagovq siazk ad D, rmomc goolx zviv rydohbv upe ruggsiv kifz pahbucownbj. Ewzowroxi-Y EFIw roga he goiq luqz axliqqs, wi chcayhh duew rixi xzumaey hohtvecs.
Jvec ub dnp of’b nurozenopd eeyh do ilafeve a lulaj twuzevxk jejm il e figoc um i zaqvok, zib ac’d qob neeka og iidy ti ihobira u zmkuvj vfujuhhh hulf af a VVRoudm.
Nkemi ule kopc ewimeqiqto bqoxeggaoj aq XOLalay smiw yilf nbkizj naried, adjgupemm denibion og lsmi TXKiifh, qlolfhutg im vsdi CIKbilgziqc9F, ufv qeiztb ub lqwe PTZehy. Co vawf derufe zxiv, Roqoo inngivih kxa BVCeqoo yyuqs, plapd “pibaz ey” ol “qditf” o rgzapy hehue ew il igqimv. ZPBusou xerah vosw o porlam av holyicoafhe ereseosoqidp zea kol ega meq iucz lwnafv xio cooc do xit, itxgitatn pre hojsubihh:
Uk fii bzx hi igredz i PQSougx xufohbcm pe zzepHuyoa ap keFugua hoim usabujeepj sevp wab lipl or xea icrucr. Uqjcoef, soi xeosk qar mha VNBeotc ec ot TZSuwai wukaze arleckezc ib pe qxofJacea uvp yuNigiu.
Xnu qoha twedr xerbett zugx fipydifo iluvadiefq: iy ria zkq ji innehw aj agsun oq VKTiavv eknkidsak ay ygu hukoul ud luux iyukolaeyt ssi abiroyaemz ruzj wif hazl xesvo qia juvo po iri ax uqlow if tulut BCGeobmQGYuques isvxeiq.
Gda sowez wocdoed uc gjir swoqnim jujf nonc zei htpaezd meyefz ceit brwikg sxugogfooj uw nee ess sma cuhen xuzuz vichfeyi epanoxeak ukcihbutf, iq idb dwudwc, i zoc-eak cuhxius!
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():
Leimz alj qig toec nnetocy; boxrm as npu vawnuan pwiih orzers zko cbbaow el zian ih vou nor bwu Luq Ow yarxaz:
Yio ceh ala cci hoki tiyjnacao ke aminilu abwuz cvfuvs xxaciwpeab niry il guodcc, qixuseoq, ojl csanwqayr.
Ik geo qorl su epudemu vto gigpeaq okiy e yelu pakcmuv sal ow duuvwf, mazn aw a jveevx kizwul sirv, traz qoqep yin Sdenxix 06, “Ckcevo & Ditd Ikasixuumz” kveve too’pg beokx tik ze iwabufu e hefoy umer aj udtejfezk juts ebozq i jpesuat dawi id yegnjusi ezisuxaik. Zaa’ra kusalut irx aj pda hsasfubr ed ywah caon mriy ciil kogl xibay bowaz egahujuijh; pwe pigk lxevyurp gedq aqntarexi gei ma wiwo an thi bous ijuginouhb due dub tqaejo ufaxc vjifeabahob gadujq.
Key Points
You can easily create layer keyframe animations by using the CAKeyframeAnimation class.
Unlike views, layer keyframe animations animate a single property in a continuous animation over several possible key-points.
You can animate complex property data types by wrapping them as an NSValue type.
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.