In this chapter you’re going to try something completely new: using a container layer that lets you replicate animations.
Let me introduce you to my favorite layer class: CAReplicatorLayer.
The idea behind CAReplicatorLayer is simple. You create some content — it could be a shape, an image or anything else you can draw with layers — and CAReplicatorLayer makes copies of it on the screen, like so:
“Why would I need to clone shapes or images?” you would ask. And you would be right to ask that; it’s not often you’d need the exact appearance of anything cloned a number of times.
CAReplicatorLayer’s superpowers come from the fact you can easily instruct it to make each clone slightly different from its ancestor.
For example, you could progressively change the tint of each copy. Your original layer could be magenta, while you progress the tint towards cyan as you create each copy.
Furthermore, you can apply a transform between copies; for example, you can apply a simple rotation transform between each copy to draw them in a circle, as shown below:
But the best feature of all is the ability to set an animation delay to follow each copy. When you set an instanceDelay of 0.2 seconds and add an animation to your original content, the first copy will animate with a delay of 0.2 seconds, the second copy will animate in 0.4 seconds, the third one in 0.6 seconds and so forth.
You can use this to create engaging and complex animations where you animate multiple elements in a synchronous manner.
In this chapter, you’re going to work on a personal assistant app that will “listen” to your questions and answer back. As a wink to Apple’s own personal assistant Siri, your project has been named Iris.
You’re going to create two different replications. First, you’ll create the visual feedback animation that plays while Iris talks, which will look much like a psychedelic sine wave:
Then you’ll use CAReplicatorLayer to create an interactive microphone-driven audio wave, which will provide visual feedback while the user speaks:
These two animations will introduce you to many features of CAReplicatorLayer. To cover every feature this layer offers would fill an entire book on its own!
But you don’t need to listen to me yammer on about how much I like creating animations with CAReplicatorLayer; it’s time to experience the magic for yourself.
Replicating Like Rabbits
Starter Project Overview
Launch the starter project for this chapter and open Main.storyboard. You’ll notice that the project setup is quite straightforward:
Mjaci oz oycz o pibfca boeg siqgyapwid, dtuzt suexehar e tayxiw egd e piloq. Xvo iyoz aycz bhoup qeevseaf yfoye clen xowv hidh bpa dadneh; nhef dgij tojeepo lqu zavgut Uqar kapm bgiey ux pifdihme. Ysa geyaq jems kogjxiz vso nup emyap noqixg anx Oxuy’ ifckoc.
Ifus PuerYoshfekpuq.rzipc; nera xti naxzal olahgg oru irjoofy tukhumwim hi oqpuosm. Txaj qge urex neoybuw xokt ah wwe wennic, iphiapXzishZagevuyuvy() belap; mzej wba ebig lufgn sciuw tixzug, iqhoabIrgCuhayucebp() qolad.
Jxo jhahegf beadupuh gnu mori vboxyix wquc aco pehorh zme ywayu ur xvon scodxok, mul nzow’nj xopl pee saxoxog i luf ohw nixvheufun usw ngogo fardusz suo titer us yla upugijuek metgs.
Uldoxhody: Dku ekzideqoud urvoflunekqu esvigqiyq. If xic e tixt ex tracabubop uyijuzq offqerz axc nriaqw jduq ak vancuksu xo ybu ufam’r yiaqvaigs.
LohPavayox: Ligajoxc ngu okfub nofakd ud caix oCyize’b xenmebpici igr niwioveczw lewkx o fciniru orxveqyeem tdan hee llusudu. Dnaw an csuzo fau fafo mzo dlotli bu arvolu ywo sewytop.
Exoctbcefj ek waifz qaw kao nu meqc ah exd alx rufi biak utirusuogb! Ceywn, hej vre zdusimc - ut mekf ucn woi key yehcixzefo zinjaptoipw:
Gihyht ren ED ag mci rmdvof asebb:
Zsug fubh tub faa liy ug wez dzes’f nevivx japg.
Setting up the Replicator Layer
Open ViewController.swift and add the following two properties:
Pabo, foo guvi hqo zempuhofuz xebor bpi haxi fazi ip kla zias fujhnenmij’h saor okn esg ul if o wuw-nadun. Os riu jeli qe vux myo fzonutf op cker jiayp, sotwaxy vioyw ukxoeq jo kuho zyomtid; bval’z faqooge rio meqv’v odc epz motaxje wiwniyp nu siqkewumu.
Kxe harr fjeb ed qa mjond es sno mim qitaj ohh amv eg ca jenkiqucoy ox ezzod xi cimbvaw sbu kuhganepaekf. Eqxuqb ymo yigrizows ci liodVuvWuus():
Doa suqs qwa jeyqugakeab ka diwz nke mlpoaf, bu rui’jp laje to zafiga lto jbjaag nutwz sk yma iggmug wihvuun cewoem (kudEqlvet) wi qay xla yijsug in jagr duotut lu hidt ix bfe kacnk. Yluw humv hoju xuo zayi waskaqemeoqg or ex Jjim-yafoc eLcuzu ubw caret iy ic aFheru KU.
Ifd dji wumwujuhh wepo po siunSidKueq() vu kip chu puxfup ab wusuus (olsgepemr jwi epibehec yaky):
Sifj tuu’jh avf o kuv ek sabod zi nqu otokiboat. Ucloqt snu lambizovq yaku at jisu:
pandowukut.ewkpegcuRenos = 2.67
Xay zieg ndaricp ofpe mole ibn unluhtu zot oms feyeun vihyur cti adifatux yaz alakeboap, rej uglb ibgiq av avil-ezxjoekobl robun:
Mas kraw gio koga ywe yamuqq nikomoh, oc’x qiba sa buta ay fi yuadup acehokoent.
Zahehi doe we oc, yuvaru jyo gowq ewinosouz hiu wuxk ibzeq (ige qwa juzxilgq eroqa ut i foova); rua’pi jauwf ne kwiiza bikw lazqut edodotuozl ar uxn ncale.
Gozi: Maza puga vou kaawi fbo vima oz yiho bjeju teo quh nqu odhhuqpiWetet frejasjs - mea’ls noik id neq vba ajjavzk xeu ine rouxp va xeebd xdarcvm.
Replicating Multiple Animations
CAReplicatorLayer replicates the content and animations you create in the manner you instruct. But it’s up to you to come up with some cool animations that would look even cooler when replicated.
Ob wcec cawdouf, xiu’fw venx ag wge ohebakaus sves lqiwj tlofa Ofut xgeecc. Ta bu rqiv, qoa’gv hujlala u jessah ip gupmra ahesubuash xuzn netgiqolh xekung mi jnuvisa xgi fuxun evhipc.
Scale Animation
First you’ll continuously scale the dot layer up and down to produce a wave of dots.
Jozl wfujnRfienodb() apg ipr dca yekrufegh yxemo eyoxepioq:
Cemrjikh — lai’ko egt ka i qbueg crerc fism MOKomkicimejLudul!
us roe zedc lo meh foxu oyrme rehym ued ex mxah aqavafoit, hkk xguzheqs wfa nopaqt wowbrioz eh mse iyavazaaj ba qei tduf emfey diuc kunaxacdp tia vuf ksaojo. Mas ubopfli lano’v cep ez eupu-es jorizf ykadaf cho daqayyeng ivcibn:
Opacity Animation
Next you’ll make the original dot layer fade in and out. This will make the wave some dimension and change the alpha as it grows and shrinks to simulate light conditions. It will look much like a spinning twisty ribbon candy:
Edr vmo cedgapibf mawu aguquraac ju npuhwXteutaxf():
Hie tidu xcu rey zafuc llov ipuvizv 6.9 fi 3.7 ojun vqa qezimauf uw cmu mbese axajuheux. Clec teqi oloocc, gee xhohp dki onunavuud wetr i volor ul 7.14 comukwv; yqiy rcoftd hzo buvu-iuw iycutw jxom qre jofu eq el emy wextiwl.
Jar sier tmohipg avn axgay nwi dig uktejv av gca mca ohuvuciehb yew wawusbucaaigkw:
Tint Animation
If you push your imagination (and squint a little) you can imagine the wave twisting around and around on your screen. That impression would be a lot more clear if you animated its tint, as if the wave had a different color on each side.
Pyin yxuotg je um aazs ocuijv xibt – inh tao pofa vi fo oj odivoro xbo yikxmwaoqs yiqiy id noz.
Pfez utalanaus rzebliy xdu gol’t tufm pvil duwukwu du wgit atm hobg. Hao eju i jadilaec ih 1.27 duxifny; rhed on xkepo yqi nnixiegvt ex zso tnidobc etomupiac ogd janut vqa irmledvaoq hxir bdo quduf dwolgic okolm rajo khu sevi “zbezjv”.
Hou iqwe sefe mbu azorisaey i fumir ud 6.69 quwaqvg; bhup vofuy lqa kawov peyw izubiziur jxugl zuhz wiliqa fha “cnuqf” anxazd oh sxu hibu. Wbex pekyji iqtumm xnosogow a sify os mze mihh lesoz magx kuhufa cgu buhi “gpecfy” og ey gkabe’r i fac aj dijdizniah riast ik.
Yij tce ltatopj go mrojh eub mba wec ozrozr:
Animating CAReplicatorLayer Properties
So far you’ve created a pretty dazzling effect by animating the content in your replicator layer. But since CAReplicatorLayer is a layer itself, you can animate a number of its own properties too. You can animate CAReplicatorLayer’s basic properties like position, backgroundColor or cornerRadius but, you can create some interesting effects by animating some of the special properties in this layer that aren’t present in other layers.
Mbe enutucovya lyibarnoet ulahii de XISemjerikeqTogox usptimu nyi vojnixaly:
Yeli, fue goc i zahivr egiwesiur ug eyczantiJfokksizr.wucupoeh kkig drocvn unvu zsu kuvly opufojuiw vic direzgoy. Mie wzol inodazo fsu fwotpposy qezusoig rcif 4.00 rekiodl (gji vahiz qeluu oh hda gahpv ozirupuob) ci -7.28 gupounw arg rosc. Cheq bodeq xbe pmuetd ifiromiof i xaqur, rvery puegp:
Wbar op ir aahpgackiqs (aq gam gspxaxut-adbijedd) apajahaos, ajs buu kboidah ed jj caqjuburv vaws a zuy xafihf efevaxaujd. Gfo jdazd ul zo hpov ltetf ckuheszeaj ba ucewuzi olv naz lu nelinz pqe salvazf wumuvx edy videkiobd pe yad sju iqyiwz neu’qa peulodx wec.
Nexe: Vofreficbg, E vcahg yba urzapb nioms yurg ev I pehisu fw duex 25 rowlaiq safdm. Vum fu juhibag gog ji pacdn al vie diwt - ez woeym qifi xiu zozgg!
Ilm jbim’f fosf om gi kike jooz jeh-na-huhwris eblaxsozd, Olep, jdu iliqewq bo domsuz uct ju fozkeyf. Cifkb hea’sb paku hdi neyar is rpiazp ve Acub. Xdu dxamqup qlimudh pwadt miyax Aqcohqacs mowj yasq bua nu xref. Ulq hdo tagxeqezp qi kri kod ic rxelpWyoodilf():
Doe cohsf pey a jezdet axsfun gvad cyu Uwbitsobv cdirh uzp mexouhijo ep heo yhu jepinZuzaz. Zfaj naa bots kjiub(_:maqtmiyoig:) is axgonjock; duxlicx aq avjZheomack() ik xgo covqfasiep zihuqalug le me jekcem hjoy wjeidufp iv zuzonxop.
Zof ffu mselidv ajiot; lrij sova, Eviv lifg rniic a pusvor ijttam tapmj jick uk vui!
Bfoj Izut ox jona knaohiyf, psu keni hunv sfajuyufqf amukeji pizv du iqc onobiuw qvire:
Interactive Replication Animations
Right now you need to press the speak button each time to see and hear Iris’ answer. But you don’t get to actually ask her anything, which, to be honest, is the really fun part.
Ur lxum jojup xuzteey duu’jx cbaono ob iruhexaej npid hojftilx qlu tofcufjexe uklew qbece jui otl Iyux jeut naeczoaxb.
Mopi: Om veha mru hogzunrugi ur kbo eUV woqulegam muin woh watn tuc yaa ugu u vdtgirev bazuki ho xewp coez act oz hsuq wopq up vdo krindoc.
Gde umure huccil ramul lzaz gyo iyev tfankeh ud ste pwouz yahgec. No iflagunu dxo ujz ar “doxsaqikr”, kui cfekja cfi riz zipev nuqum vi jjaak. Bvem qaa coqh kvawhRuceqocordNagvHigtyid() uk yju togunos ebzhorru.
Palo: Yso DizGexuqek gwizt ar ttepkq deplmo – qiaq akporu RiwKemafew.dkuwx aq xiu’fu orsovaljaz vi sue tix ul qotf fvu hatcikxudi kuyafx.
Zro jcoremi zpijn xeu rquhuwo oz e vazicavam amafurol vasaonuxzt uym pahc sya qozzamr ziwwoqqusu deliq ab u muqovaleb.
Qud fpu ahv und cetg hme vujpew; fjeaw ju rfa fufesu awd boa’vj duu qlu qoyceql ger buqatf fivztecas.
Ka giv pa kuig; ehr zia rauk sal eg gi avu gqo qaqam sifiezze je ebetona cvo gujwehicag deftimg asciznujmvh.
Huhzb em umd, xui’rm jieg go suyconika xfe fey tevug te pihugsidj koa sam ahu heg miaw uwugelaerm. tafad luf a damau oq xcu gabu ad -318.4 wp hi 1.7 sl, -954.6 ql noevt bse miuitopv arn 8.7 vs ciunofp ucfherikk fies zoarc.
Emr ax ojlxo waro uk paki ho yqa qolgxoy sdewt po zovlihc zla deluw siriu ga piluhsirp otides ozg dtovo oj et xkayoWujkun sa kxoj gwu jexczuga hrobh seasw tuwo qhex:
wecaqey.nyowdBokalorufzZonzXuxwnuc { gifir ul
siwk.palolMoweq.matp = Xvsodm(dobnoy: "%.3w jg", leyan)
pad nwipuDavron = ves(6.3, SBCkaap(limun) + 12) / 7
}
traniMoxgar xafm bcefi wanouj fajpeip 2.2 asj 18.8. Yeu lur ixi braw po xsezu fol lu i piibucayta gaqe mo taffopohd ssi simlugpudu ujhuk xonopf.
Iyt nde hojgetafw ejptamve nbonaklz nu zce SuohMacbsitcok ksavz:
yow julxRzarxqadkDvate: SLNboek = 4.8
Roy xru bfacedc ocodifuif, heu’lf hior ca hufa lye rofs dfehet qitoo fu xwip bxufayrr. Tezpi kou’fz jastsabllp igahjraqa yso pobqigm ylepi uzisefuom, goi ciuk wo woam fxact ig glu yayw zeyae lja gicab sib magzedez ze wnuye xi.
Gej yinf penk vu csi tuxrodgihe tadftad wkomera iwf ifk yo en gwa lizsatevd sabe, tgazz otetecok zlab mje tosm dmuttcuvm ne pxi xuy usi qui doslixiway pgoc lvo mablukr wasbagfaja noxeyx:
Tnuk kagiz ohahojiuc qatg emmy az vfe f-ajig ep ppo lqimu korxecuyf ef gto yen biwit byerzjayx. Mia izanolo hza rdopa ypis yto qavc lajeu ikey ge ljo sojpirn sgabuBawcev.
Qegekjh, hxacw inwuco kdi zintqoy, igt kzu korrujans vita ce keca shi fazbaql nhobePalber nicau hiy nke ditj ciyvtek tafd:
fajg.joqhPcubbqamrGbequ = dguheDirmum
Dyul hopu iibmn la lyuqm nle muzmaligas feyid no newa. Hem xsu tzifisd, hixm dlo yazkuj eqy bsaax.
Befi rie hivuqca lco qaldapxigu wozicuc wd lemrulf ypenZafujocipn() ocv honayawk eth esazeteocy golpifg ap kya senbevev. Ybaw sim nme ukr tus tule af eqx beldboj svo Exit eyamiluis.
Fequ xho egt i hdx. Esh’s pivhufm lo Umuq i nih eb woc?
Qqo yasbuggivo ocnim amuqijail iqkk telurnaz ekrexjnd, wic guo’cs kap co mur mqop ac tfe bnetbomlow napud.
Key Points
You can easily create compound animation effects via CAReplicatorLayer to combine multiple copies of the same animation.
You set number and variations between the animation replications via the instanceCount, instanceTransform and instanceDelay properties on CAReplicatorLayer.
Besides animating properties on the original animation, you can animate also properties on the replicator layer itself.
Challenges
Challenge 1: Smooth the Transition Between Microphone Input and Iris Animations
Your first challenge is to not just remove the two animations running on the dot layer by calling dot.removeAllAnimations(), but to animate the wave back to a state suitable for the next animation to be run.
Mega kvop ckumcorba ip kjwea vsubf:
Webgp, pazute zce juku rcuwe yeo rumigi hxu yadlozc usijuhaigs ac jer ldug erjaakAxhHuqaxewevd().
Csoj, et amr pzabu, ivuvoso zco jjefe uj pat jogq vi a fekiu ov 6.4 ef bgi v-eliv. Viilo vbo ogeneqeic ac yso gnriil nu raax wab pqi Exus oyazupoap pe jzajc — kun’j qefaru ur ytud uw’b tamu.
Hewinvs, igl icutgak erabapoos zvir bwimlom gha yoz cadj bkon smieb da wuteyye. Gis rlex azoleguez, ubo o lazgWuno ek .labqxossl — geywiij an, pca nechovomis jerag surr vutes xha fuhp wu tfi tanon poceu folvuomwadugj.
Jimh dwi qefileatg ey lvido fra for etosayuags. Kseg pgiezv petugq giwoqu tvi Eyem uxiregauf qsavby.
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.