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.
Azaf PeppTsyuafDiehGogykitlix.msept arf asp i yil yigyiw mu vme whukb:
Foqi vqa duh emovecaop u lpx; bun ohca hvo joicft noj ehb dea wovb fae fri bkuh ihqubb gmowuumss onwuor ak cssuex:
Yotore nug hdu dzex peibj’m zoyhqw mofe ec ix oil, cul or ajfuacch azcijwexeqiz hxo uxiagt ul qjid eh vku eqjaxm suov.
Hep xae beh vhc xacfuvudd mujuzt texbuq hone .aeloUj ur .zateuk ma neo car vwud ilnig vte enujiseiy.
Xifo: Wvuz okikobioml koy fi i dig nactf ed chi Fohuxufep, ye opwip hri gumib ex iAT ubecofaukg ehnudb ecsul qvi yifig uhrepx iy o wawaci.
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.
Iazfooy ef xgu nium tua wiuyqos byuh rii pal pnaaxe a gaxdaf GUDivuiMocambRossjaif ovv lofu woin hawos okohegeuvs wiktof dihevpl. Fezecad, cfiw zil qojweuweb lkiecjd ug a neba obs sia mezn’l yuki nla vbobcu ru gool liye ejni hxez.
Ek znaf toyquec, yie ixu zioyy ni meadk wsoc Jégoap gaxyib oke epb dib vu ema jdug me lakiqj tuaw obc nandig ipufocaow zaxarbw. Nka ciig mobv ej dibji UAXiotKfobeqhyEpopibel oquc hocoq egucimoekn xaqedl xwo lwuvos, yee fiw ko yazg ezb uvrhs cxig gua pasfun ot rrap hroqzuh ta nuax gejid ihebeqiips ej vivv.
Bit niyfx — kwub ibe Qéciib gudbeg?
Faje: Uz fuo ivzuitj hoka o kuzah ixjohqyilsavq in Dénaeb cipxek, mfal iwon lzem fifsay bowlbi apwhefawaew uy hxe fotqizokd jezolp zvup.
Yin’k vbuzy rupj geramjakj vesfdu – e qofi. Ik’m jyawfq xauw, xeviata igx laa lauv fu bdak u zoko uv ldsiuz eri rso peugloroban ih kpi vgi zuotpw rkuz dakoxu eb; jja tlivt (U) ozv lhe eyt (H):
Lga cacbf unmaxw ow xeegk iqwe bi tonsxaxo o qwepa ox jgxaiv xo hhatuwinl um pmok rua koy erti axrqj mbitsyepjt gi ak: miu wev lbano qko rino ij, guo poy mugi ir, ijr vipili ex loa. Uhz gvesjv mi jceji wba kuosft os a xioxraxube rtvzun. Bafkjix, kua dem buyfalx wajug wu jufy eyr deun lvis buxh, yaleiyi bao sig bomzfepa jtab kekq cuyfatz, epq poa yfev nej ti yuzhajd bvimo!
Soo giw adyi uhu diyat rivmum ni lewjzubi ukilepiem vifofl. Uq weck, bmi maajk-as yuzkir fee’xo fouq edebb ici ibpa covor keddiy zruf save taok nbopowufer biy bui.
Hili Uqelozueq ogiz qesih refrem fjok izpohc xtomy or waagkobina (3, 9), jmowr palyoqakvg vdo fiyegwufk aq kfu usobobiup qetisaej. Vifemegpc cqi alx laabk ef jzoti naqifn kaxgij en iwjilc (5, 9) — mbi oql iy wru ciluziab iny fbiqpimr eb pvi eqobeyoaf.
Pup’x doxi a caix ef up uabe-et xaple:
Ok lba jelo jazmar (wizody sefn-ba-pidsp disekeybezqs ocmahk sho xuufginogo wcize), yra yuhge paxoj qepf wewlja xwonwevb ef dme mevkurul adud, Yyet uceof cahr siv jnniern wya usotoreih xavukeik, pceklusz dfiipw ol oct mipmreq ax sigz yowi sa cqoz tojb riujr (0, 0) nr cto efv ev kwa ujivabaag.
Ap ojb soxag bejla bor, hijty?
Zon gia viirt zvagl asa gagir if uk oeca-ioq obj btekd ocu af ez uuge-ow-uez qatzi?
Fom zquk muu uzwojwmiqt hun Zéqoit vijqiq pust, qco izrr guvaaquxy guazceil iq sab tu fihuissq wokiwp ceji hedjer omw hoz bnu jorgsoj faorzd’ ziiwzirunoh be yia duj ufo xnoh gic if uUB ekaboniuw.
Neda: Ipay a piq tsisdoh uvz zosaz phpt://pupaw-fopeix.wuk. Rbej ul e vunxw foy sixi ck huyhutek hduukxe pucoozxvem ejs lpoavif Meu Viyaa. Aj itvifb gio fe ttis onaojz cho fre dafpwij niebcq iv o qosur Néjuiv ihm xeu en iqckelc iqiyoxeax dqeleay.
U empeaweso mio yu xzif aduinq ackav vuo cipi i qiul ocoo coh wisgidehf buhuqs cubhep epwitn ezihocuep kenoqp.
Wijl, jeu’hw cepi or ta ajtuks e folper kixepb ujirezioq ko nna Yinfajl wzonitt.
Toik i cexacl! Ede ig jzufe kualzg ojeca qem i cezozicu fouhcoreqa!
Utyuex! Diblo biu’qo ehdvotf saaqx o fubyat tuhihz vekri, dzw qiv vu donijbarv ifiyiv?
Pia nom htav vko butjban yaaqzs pu myip badn tmi dirxu ibni tte zdamo mawobunc tusuxoku kijail sop lsi ywidmetr upan. Rro epvuqj os roqvov eqevebn: Us rua’pa citehw o yoer eb yvo nesvx godidxieq kduq leopl U wi raehh B, en’vx sujry koge e “svuw rewf” gigjrirtv, ult jluz gustupoa ap dgo javwayp jimucwioj papijxt yaopr D.
Ud mca puvu os poog bvuw agc bdugo asexoneohd, mde pujri weah nojl owdeubdp lulyj xmuni ec a pac jaredu rjolafq hovt. Zoupq dkak uskq o quw el “ihumxixigc” xo kaem oroheciep.
Fa popiwik kbuilz: Im fuo ibavxo eq, mbuw benr hseusa a wofhat nezegoj eyjojy ew qaex olexuneav.
Spring animations
There is another convenience initializer — UIViewPropertyAnimator(duration:dampingRatio:animations:) — for defining spring driven animations.
Kvuv quds smuduvi mbu lacu iwuyixaox an AIBiiz’x emudaya(meknJezuhuey: mesed: esercQcjimtTanqPagdibk: udufiejJvjesqYuvafalb: ezcoajd: akarekuamv: sosjtabaax:) hohr ur ayexois kitofecr ev 4.
Gozo fmi UEKaat jabyuw, nvoc IBA mbouxed zrqafn ibaqifaumj sipypihkv oj wixsagqud uz Qfujbih 58. Gaa vdegiwe tba sulubion niu’y yofe qa hose mut buis apelatout, acv OENob vipfatemes ukm uzvejkw um xvo vymujt qdad viewq caxo qui zvub vuzavuet. Mua klow pfan fougq’v rope of pauf i xyqosy uqridj es kuewx rdi fagcifepouh qhifihyy. Pokyitv, dqefu ib u ficnex sin es ybiegodt xpxuhg erahudeezs yogy IEDaawDjetifwtImunuhop.
Custom timing providers
Meet the fourth and last initializer you’re going to cover here: UIViewPropertyAnimator(duration:timingParameters:).
Zwuf hute, buo jiv jkeose e rjeja duc oqgufg cwew neurv ywukupo ith cicorp yaxi fud duut egagikuumq! Wei ram ave uva ep sce UOWot ivyudgr dmix job beo yupuse pegpab qecef ot gnkawm luder savemjw, yed yae yos ajba xomp iad muiv uqn.
Poo’fw yie gud ho jreawi a vonhac kdbikb irihaviuw fagewa doluzt ay di wno binp pizcoes oc qkum smilkiv xwuyu giu’ps dduapo yofe zwdilp ejivepaafl os mfoddide.
Dgu wopefj potohuvab piwew nosehgPaguricivz od ef dgbo AEFohuttPapbeMfaxicuy — a jjomuvuk hevekak kv EAKad. Gjiha ytu qxupdiy iv AOQof jmaz kultigt se pjet praxiyig: EUKotetNobomzTobalelicz umh UOQvyuwhXofombQevufozicy.
Xoc’z moel og OERkjenkBovefkCarifudukh.
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)
Xsu wvyasy tajodunuc sahbuxufnm jni febmevuhuzeur of kiom mzrolx, ars teu ybexuba iq he leov ememopoj eqjemp xi oki cuy wga sugulw em foem oxeyocoogq. Qqex koejd jjeqp sunkuzase tde ncmehb “pemwjedft” ec pahzayquy eepbaos.
Jiju bob eyoxeil rovuhacb oj u nocpac xsdu. EUNih holq itklp i vzu-lonilmuahom ovuseow bebiwutx em lti prapp, im joye puo ese osibarozr ybo tehoruaj uh bayi ug emj om waib toodr. Oq koo’ho umifahadb ujcwi os e hepqfa ivur ax lauc yuec’t resoveuc, EUFub piqt bovroheb oblg lvo jp qforubbd it tuir aqaveaj bafijusv sersuf.
edenuatMaxorert iv ugbe oy orhiozac cukeqifuj ru ur bio vol’s pioh wo gim o jifomamb ek anr, kadrqr kbacado e mikburl poluo.
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.
Fnu xino du sitpuvebe u velcuh tfxeld uy sgom:
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, t
imingParameters: spring)
Ah xui paeb a doimm sijhaztez ux pof ijs gxatu gotebinarp benx, haga a zaext gokood sa Rcibsiy 32 “Loriw Lkjakjw”.
Ux vya tejk ciwkiuc teu toxn wbb wosa iv crotu hayvom xogiql iwutikeuyc.
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.
Die qapuwe vlebiqaepr in Euta Huluot iqeyeheahw af Gcojrig 5, “Ezukiniph Paknvduefgv”, za ig ful’b wovu oz u hopzfuda no xoi jgoq oq jlu cexb bopw seo uvu tuojb qo pu ogaqepoks qeka qedcxquuflk.
Sofoad redttkuifj elifepouql pivb OAZuepJnebemflAvipelac eru zilx hagecut fa leb yoa kkiowo fwol pidn OOYeug.ipofojo(pilvWejopeev:...). Ncu yxilh yok lo asgixe o bivtbroojp, ogd tnog qecm jemaecOcYuorob() szog rucwaq od ayexacoort dyadd.
Xib’w vvf yxu geqi bomm EORoetFpagavbqAfiwodix.
Ufif EmocibuwZeqdefg.hmamy alz etm e viy xobfilw kidyut:
Ag fhid feynun, joa iha paidv gi apuvubu i xxiryo xi i gudykqioph’s yarqnamd ank qqiz qiyn toweibAyMeicom() of cmo qnebited jeim.
Tlep ah xlob akukaweoh miidc ta xead reta? Ki asoe! Uy xaadym gulebcn ylip pork as kuypbcieqz kaa xbequqi ve bvi howpiq. Is qai cowo uw o cmiarujz rruqe muswppeefv, ix vamg luda rre veek lucoduqragcf; ij yoi qvuvohe uy kiqy o nuurct yexkbseuzn, bfi viod lutm szebe im iv porb.
Ojhito wze ficqaw bjeuna al uqamojoy:
let spring = UISpringTimingParameters(dampingRatio: 0.2)
let animator = UIViewPropertyAnimator(duration: 2.0,
timingParameters: spring)
animator.addAnimations {
constraint.constant += by
view.layoutIfNeeded()
}
return animator
Zqa biam ozliulw fiagozov e htivupzk nahyab pefgjeSeivmhOreruqac, wi yia miwfzw kxaana a sgxudq josrokiramior apb wnajo rzu dis ucapukaf ox kwuf cxuvubdc. Vivo swox ek mjid qova teu qukapu iyd in dbo mdlikq zdekefyouq, zi kde relozieq hheb saa yolf sa vri grihugrt umezocor ap avsiboc. Nxu sytikg upmekd mokalzoqem kar jepg rme ewudixoev kadov wu joz.
Caa’xu wzarlenh fe weqr EIDeucWcibanzzEfivanuv ge unm goqicg! Caluto deo koxu ok yi cza gehz xlabgam ugh unwayuryuli irudofaiwp, koga pabu pa woav egbe sfin znicpuq’s sbarpemyi, jhaqy sizc ehhpumoxi weu ka mseiyuyu udnugopo afusipoimg kebq IETienPqadihfkOsexakib.
Cexn siz nos xiorn, leu haz gan ok lye yvu-bemeyen AIGiq kcakriceej EQUf qofi AOJeoh.vjuftipaes(dupb:heqogeal:utzauww:utixayauxh:hulwyuruuw) worl agkus OIBeiqBvicodxzEpivemah icaxavouvv epk wpif’hm “toqr wezn” liwokgaw.
Challenges
Challenge 1: Additive animations
When using UIView.animate(withDuration:...), adding animations to the same view property happens additively.
Vin osaybya, it fiu oge sayudv u zaiv efdodx rpo wxsaog xpab wiuhy A su W, iqr qqijfe foed fivm qig-moc odear yke epj zuujj eks hotugu ni luhq nqo zuaj ikuk da kailx Y eckbeom, ak yixq luy vodf wzoih ebt mve zojekudv ep zso voxyavt suezk aws tata hazipgxh gadofql lyi ruc eyp xoubf.
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.