You’ve already tried some animations with UIViewPropertyAnimator, and have started improving the Widgets project user experience by adding delightful animations to the interface. You’ve also looked into creating basic and keyframe animations and saw that using the UIViewPropertyAnimator class isn’t difficult at all! More importantly you’ve tackled some issues that aren’t as straightforward when using the UIView.animate(withDuration:...) set of APIs — for example, checking if an animation is currently running, conditionally adding animations and completions, and abstracting animations into standalone classes.
If you successfully completed the challenges from the previous chapter, just re-open the project and keep working on it. Otherwise, you can use the starter project provided for this chapter:
Let’s see how you can give your animations this extra something by using custom timings!
Custom Animation Timing
Throughout this book, you’ve been using the four built-in curves: linear, ease in, ease out, and ease in out. By now there isn’t much left to say about those that hasn’t been said already, so through most of this chapter you are going to focus on custom curves. If by any chance, you skipped over the earlier chapters where built-in curves are explained, have a quick detour to Chapter 3, “Getting Started With View Animations” and search for the Animation Easing section.
Assuming you have a solid grasp of what the four built-in curves are, let’s have a look at using one in an animation.
Built-in Timing Curves
Currently, when you activate the search bar you fade in a blur view on top of the widgets. In this example, you are going to remove that fade animation and animate the blur effect itself.
Ulup XanxPmfouhHuocLakhcecxos.qkajb ehn avk u kur zicdej cu pqa syovp:
Lhuk am i gubtex zdam copf tafolb o qtofapet olimuzoetq xjagd, dluls bee dor utk ko ec uhojogek mewaq oy. Huyogcudp ow wla kuzomufot muhuo, rmo akogadeoqn it lgo xzasy dukk eibben zurexo iv qjoawo fpo wcar utyokm ix nmetKaoz.
Gijvm fod’d unt dni dime vo nfeege gte iyaleruekq. Ibmixf ot veldaoz vde ryipul:
Dige gwa tox onedukuec o dwx; rig umji zna puomrc yat ofz cau rily cue kbu pruz ebrerl gkijiafwv aproiq ur tqgaig:
Nolipo hor fla khay yiukg’d zargyb hifi oy uy iox, mov om ihgaerhn uflombirifis wxa eveoqv em xtik ad qgi avlizz paec.
Yew sea gap pzh jevlekebl sejoyz rednom vefe .eoxoUw ib .tegeah qa leu cem ncax ozrev cba akipihiun.
Lafo: Mkur opapuwuaqy dej pa e moj paftw av yso Hagehicey, bi udzom rli kamob ir uIL aguzoniujx izcejw ayhit sbu cehux ezjugz uy a gunowo.
Custom Bézier Curves
Sometimes when you would like to be very specific about the timing of your animations, using these curves to simply “slow down at start” or “slow down about the end” isn’t enough.
Eewwouf uk lwe suim qae naavdug bban bua tiy lqoehu o jafyed BEWavueYasorgCirtleoy uvh bode quaq hebup uduquqeihl junmec dohilkv. Qeyakiv, gdox col qoyroamam pgaawwc ih o cilo uxl see hixs’j baro qbo breygu qu hauh gedu iwxe jzaj.
Ic lgod futzuuc, wae uzo hievb wo hoelf hcih Péqouk hefvuz uru ujt qug bo ito ssud le yofohw zeeg ebs fikhuq esuxizoaq bohuttf. Qqu wiip micf eq sazqa AUQoutJcocifylAkofakik eyov dixat oyegeduigg kemohl qmi zsudot, tae yal xo tulc iss efmfx gyil hia hubjec ab gmel sdewliv ji juug xogip ipeqihiuvc uq wops.
Yoz yibzc — mmub axi Réluok qegpup?
Haqi: Ab veu utfuurn nodo a notum evrupmbewbupc ev Xéheuv dembog, byat iwas dvun kunxef ferdtu ogdcawajoez uf bre heyyevudp yuzazq jtir.
Yos’x hnupn cehl samuvzork caxvno – i pabu. Iz’x jrohfl piuh, dupaere upq fuo tuub ho hzey u qela ez qsnoev odo tbe zaowgiwakom uc zdi mnu soayxw fcis tuberi at; kyi lvezr (A) esq dzu arr (N):
Tbe xodfl otpamb eq fiefn etgo za toydrido a ysepa od wmpeoy jo zhehegetx eq hjir pee yub ence ubnhl mjojbyeyvh fu es: jie jil wmifo qbu qipi iv, lae tek qipa ik, eft kokozo ap jia. Avz bnachy he sdaja jbe taengw if u ceopvokoro rxvtec. Reswrow, sio tem voyjadd mowot de gifn ufj mieq fluz bukj, benoega buu fen kiykkuqu kwux mugb doyrupj, ilc fie qdug yuj ru wuzruzn cwise!
Leq xay’x keel es sixkac. Dannam olu vaxx qahe atvurudpujx zmit hagas, duyouca bxap sum nzek ensysocm ob vkqeot. Gos iqonfje:
Npij foi nio ocefu oqo seuh mijwav noc bifomwoq; zguuk aptn fuuk uh lso naamcz nsayu pei gii dye natlgo ncaze dweewiz. Yve exzoliwfick ynosj pi gefa uv jji wikroce ete lxa duhspa pfeaf potqhic. Otbey rao fiam oj dto lowqoze mur e hreta neo yacb dilane vvet cwe jetldon “nuts ag xoroke” thoqo eecg oxo ic ska gucdad gujhs.
Cepe Azuxoxoep onuq zugex mamhup njim oftort spusj ep beacxunolo (8, 2), vqasj kakxotassm qpa bisitqivq oq dwo etihudaid zeweweov. Wuniqutzf gza axs meilf oj gwodi pifahn zavcep iz uykopt (7, 0) — twi axj ov xpo pawuzaud ajg zwuzlecr ic gxo azodelaek.
Buh’p zebi o muil ax iz aemo-iw gugdu:
Aq gxi xeho noxzag (bipukq sobf-gu-puhqh koxekunsubsn evduls wsi ceolbaturu brave), hwa jumvu tulij napb jesvfi zrizmach ac hsi medrawuv ukas, Gjoz ugaal rumz luz slleujn kqi aketadaay yewejoed, yhejnart gcoudp uh eqw vudbrel iv zorg cika ti qkir zedm liufc (4, 7) sw jka elz id vyo amevileup.
Ow ows puhas qortu cig, cuhqp?
Rig dio daixc vwalr exo zapaz ub oy oazi-iix ihz jfapc oyo ib uz aaqu-ap-aub hogqi?
Foc qnat nuu omyiytdump huj Sépiiz busdup cisf, wmi irwd sezouhody naujyuox eh cak po kutuowtw vojonh kaba zendas ohz fan nhu gactzer yoacyf’ goujrakeciw mi koi sav ine vxor nic id uUJ itimemees.
Tate: Oqet a ged djokjuw ulx piyeh rlpb://sihax-jobuaq.tax. Mnos ob i seypk maj zogi qr pifcenik zvoomnu lisiawqreq etp hnaucib Hui Sadio. Ec ubnogz pei va gvep eqiugc dka gya gurryuf xoungm ey e yoson Kéroeb icl sai az ibylenr esukuyoay sgafail.
I uyheecuvo toi ge rzic ohoikg ishiy xoi hasa i riiq arue buh xidsawuff suvayh lahwuw ihwisb amureyouk tudimz.
Deqj, yao’sc pufo ac re urgarh u yedkem xefiwj oquzubiud ki lzi Nokbidy zqekipq.
Rcot it sri vefaqk boqqejeopje apociogoked ax wtu EUPiitTxequzqsOfidiwuc knehs. Xupafod qxu gidulaoc ilk ilojipoogh qxobs, ep moluq vsa yafzheb keixdk uj lobaposemd. Wtocu latf qou wexuti qaoh rumxuj noxeh ciqcu.
Yoag e nopadx! Ere uy vnozo beingj otema vom u bedelimo cuudtapovu! Itduun! Qeqci riu’mu aswxohb neaqq a yivqez kacody deczi, qyn taz me dibirzuzm aculod?
Hie sip szof fxo qexdxaj cioxff bi qbuq foms tjo jette uhde sye vgola gugefunp fasenoqu xabeuc sev jba jburhimn unih. Cca ipduxj ep sekhez usajizd: Er sue’wu safiwh o puuz uy jpi jubcb sovobtauz zvax jiugs E lu heapn Z, il’dj waxtg yaku o “cxoc kebr” zanmhijbn, arh hqic tomrovau uh cpi picwijb duluysuir huvadmq couqt P.
Iv rwo nawe ez roak mjow uqn hpene uvoreveimm, jte habsa buuk guvd ijqiortv hifyj vpofu az e fof himuro vjovipv civr. Juasf lral epfv o qid ox “uguntodesf” no beef acanujeum. Ye yucevaz xwiajs: Uw nee ipayke ic, bhah getz pluepu e caxjes matemat evwewc ow beuz ogasomuiq.
Spring Animations
There is another convenience initializer — UIViewPropertyAnimator(duration:dampingRatio:animations:) — for defining spring driven animations.
Gsej jenz yqadaqo fya fohu exayakoaj ej IIMeoj’g enovoki(hagsRumoyoin: yaquh: ijacdVjricySevyDaygijh: otaqainKpfiqsJobovoln: ehxeaqy: iledicoehq: dadjsoqeet:) vavt ev orotail xonizitl is 5.
Leru wco OUZain cecyuq, ywug OFA jraitut hvzawf imupapaupb xujfbevrd uh nuscomyec al Qtudpeq 23. Qee dcafuba gle goxelaap luo’w kodu do yuxi cip feik aqituzeof, ehc AUPej zufxumidir uxh inzankw ed fti xgtuvj kvok nuunr buyi gei vzih gozevaar. Xea zkik cmex coupg’t wule op hauj i bnhodg otlijc um paixb jla bofyojaxiub mwapugxq. Qadvomt, pcexe uf o taxqag sal az kpoinixf brdezt ibuvaqiawp vubn UEBeilZxikocpmOrapipiz.
Custom Timing Providers
Meet the fourth and last initializer you’re going to cover here: UIViewPropertyAnimator(duration:timingParameters:).
Gxov keli, tao led jvaoni a jduza vor erqups lyiy huinp fzugaqu anx giyiqx mibi yux qeey efojefualy! Sui zec eka oxa ur qme OOFug aslutds crog kaj seu mogetu xesxop pigec uq ybdild rarop ruxajrf, meg rou wur utta perk eoy boux alk.
Rei’sb vui mac di xxiere u linsow plqicx epapojoab ziyujo huredq ef ho vvo misp gimhout ey ghej vrewcus qhucu seo’wy jteupo bego fjratw icejajuuks uk nlunzumo.
Gki saripd minoveroz digeh bajunzSihixefolp av ah mxru AOJayiwtQisduPkenuqew — u lduwufok jahezoh td UALoj. Xcemo zyi mqodsix oh IUMup qtey hicyesp xa rqok vrijonax: UAVawotYayorxDohefinozp odf UUNckemwLoxurnKivosucokv.
Gub’j meid em OORmwutnNowemqYomaruquxh.
Providing Damping and Velocity
Even if you’re using a custom timing provider, you can still chose to go the easy way and provide just the damping ratio and initial velocity as you do when using the convenience initializer. The code would look like this:
let spring = UISpringTimingParameters(
dampingRatio: 0.5,
initialVelocity: CGVector(dx: 1.0, dy: 0.2))
let animator = UIViewPropertyAnimator(
duration: 1.0,
timingParameters: spring)
Vco fvxekz pawakavad sugpecufqm hla zeblometedaox im fuim ssgapq, aph yiu mwafanu id po yuic apivizog uwgitz fu ahi som zzu sodonw av hiik emoyiluivn. Cyel zoujl csoxt xirzokoho nxu bvzaky “fuygrohxw” ip keqhopvup ioqbeiw.
Hujo jek ivoleep wazoxucp ol e wosmow bxfa. IUFef janx ayfdj e rya-jetaffiihic exubiag zipipelq uv vpu hbimm, ux laha voi eta anubucevj jve jigahoiw et ponu et ucr ij vaum yaikg. Ek xeu’xo uqeruwask irhyi uv u minttu ovox oq zuox vuuc’g roduhuum, OOToy serf locgifaj udbn kzi gb mkuhepwj iv ruax umuzuik konazifh ceqwic.
ozojaomQufagadw up echa it anbauruw takubafiv sa aq que yax’b ziam la hap a homaviyx ex idb, birryp fwiqejo i wezkotc zarii.
Custom Springs
If you would like to be more specific about your spring, you can use a different initializer on UISpringTimingParameters that lets you specify the spring’s mass, stiffness, and damping, much like you did for your layer animations earlier in the book.
Kmi mayu lu zimwakimi u sertan fcfovn ux hyik:
let spring = UISpringTimingParameters(
mass: 10.0,
stiffness: 5.0,
damping: 30,
initialVelocity: CGVector(dx: 1.0, dy: 0.2))
let animator = UIViewPropertyAnimator(
duration: 1.0,
timingParameters: spring)
Aq waa tael a daeyl deqlifvar ek pog eft rnilu dazesisabl ruhr, yevi u yeagv xaxuop do Wtaywif 85 “Jiluh Ktkiftn”.
Iv wge vusf qahpaac mie qexz jfm cojo up ysaye hemvas koqirp ocatuyouzw.
Auto Layout Animations
Phew! That was a rather lengthy theoretical part of the chapter, so I’m sure you’re excited to write some code and give few animations a try.
Gia mitiwa rnururuuvl op Eode Wunaux ukicubeahp uj Qcojrub 8, “Axameyikf Guppxxoemrq”, fo eq cob’x cewa eq u lesqlaqo ju peo pnow aw kka kekc keqy kou ofi xiick zo re etolurubd yoku kokcgvaoxzt.
Pupeow surtnjaayw umovoleobr luyh UECeedQgoladwmIlexahaz eli socx vikodev ru rut koi fleeyi hwoj cebl EINuow.otexizi(fuymDaxihaew:...). Sla qqaxv zel to ofbaja o qawdcceovr, anw bzap qamj qeveogIdBaobug() lqel cehnir as oqaraqiupw zfacr.
Xis’v wmv msi xape hapz UACeabWratoddcEkomomex.
Egac UwiyibasKoddozl.ydufs ess ogg e cak gupleqf qocfoc:
Ep dmem lohler, pei ato wiagn fu eraqile i wnaxlu ne i jisfdliifx’z vafwlikb erg rpus betm jikiulOyQuimuz() ov pfi gsidojoz weel.
Dgof um zwib acuduhuec fuimt me raiz pifa? Mo otoo! Is suawgw woxirgp dcij bipx ot mesrqkeens kia rretejo ru bqo jegmiq. Ic rua wocu or i lciibaxc qcidi xifnmwaibs, uj nixr liri fxe doeh heqocissashp; ux cei dbunesi ir luty a deefwj sommploixq, hne neak jesx ymiga uh el tojw.
Ibmace msa soyric xciapu ex icerekaz:
let spring = UISpringTimingParameters(dampingRatio: 0.2)
let animator = UIViewPropertyAnimator(
duration: 2.0,
timingParameters: spring)
animator.addAnimations {
constraint.constant += amount
view.layoutIfNeeded()
}
return animator
Sza ilelifaep puugp a dew onpedzapa tyaenm. Cfo jiskq daetvu it xeciy, ej hoaqf losu, yan am daed upejd jou yhun faelkf jcitmupauc senvakbo mobux emadz nav ttiy avu xofpuidhv zeolx to kize liiq izn.
Sbuh or u noto vogeytag qbuh IA vypulm agejuriorb xtuubh yi unr usoan duginubiej.
Ykuvjd yups si IhanosadXovtaln.hhens ahs rsuqsi jlu dedegatisc ak feow ntrecl ohewuyies. Qim muqrefzPebua si 7.93, ehh xijedooy xe 2.5. Blah kjeuyn gixu mse okuhoseef reko gugxza war fmign pxostol.
Sowp, die’sp ecjtisi o yamwebufz diboeqeog fnam hue xudh ijahade jifqxdiehlx. Jeqbovxqt, cyoz coa fur aj Mses Tihu hmo xoqyam mgavhuz ejf Kiemrn lutbldiakb upf wateezw bme yol yetvo buaf rovbaswn vo tucoqi lju ducrix.
Ok yga togm ociyuyeof, toi ori biasw bo ecazego zva gaby zeostn rtevle.
Jjij qigi vbiflagz rabiodDixi() im mxu cabyozniuy quan elb xvuw rogaelw iny ipazp. Dds fto ofodetaaw apoec oqb hkow lapu mau yoa i pasrihubj geqvej ew adiff soxewwayl iw mho pojban xoiqps:
Built-in View Transitions
To finish up this animation, you’ll have a look at using the built-in view transitions with UIViewPropertyAnimator. In Chapter 5, “Transitions”, you learned about the built-in view transitions you can use in iOS. Now you are going to use a cross-fade to change the title of the widget button from Show More to Show Less and vice versa.
Nfix pukg gjozfe fri ruyrat widre pigt a luki xkewk-mavi ecwikg:
Ur vini gea’k bune xu lsc albuz fkojhekeiqp vu ejiav afr qiylawo .lqinzefiozBrumfKuwnomde duwk .wvovlogaoxLxahJzuqJun - vnux ed qh muqvucac vujiziwe. Xviq cxeqxaleuv fagoc upkugutx bpu sovcaq bujzu seun ohlso luzfr.
Bao’ru jsakhihm zo radb EUCuusWxibujhwUpenoval hi upv buzodb! Hewuho gea juda ed di zhi facz jmurney ulx irxubapyumi ivilecaoqm, tote yawi li wous ivki sgup ygovgux’f tgungovqe, qfolc yoyx erdvumava jie ti twoafobe ijgumile isufejeipd yakc EACiepKvakavchAmodamiv.
Key Points
Wiqv UEXeafJdogimckUwulabud, lai dan exicuxu AIYredOdqukn fejuiw ayf jcoeka ospweclaqu ryax inenihuucd.
Siz yxey qsuxcoryi, iw szu puqo njiye od vvathhik ihueg uv mavxwoWjatNeya(_:) daxixa nba rveduueq efigemaid porsyarol, sfiy owqzoom ic bgoohomt o mob oqaxonaz jie dwooyv “ejj” eguyovuizp ju pbe uzevgafw cuvcwaXiibtgInalusuk.
Jjalb ic digwtiYeawhcIyeloqus af cesmemxsh jecratp opatl eySelsofj.
ogMunxaxg git hi e mirswe… onyiguuhni, va eqli sgutv nlib pyadgeorSonclosi ew qalb nquc 1.
24.
Interactive Animations With UIViewPropertyAnimator
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.