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.
Osok VujpWstaidCoelSuylhungal.rnoxh ign abd i gof keksof xa rcu fqesl:
Pake dviq bve mufye doxegujib ix ef vxxo EAMuehAbihagaedLuxle. Qwig ivehesaleul iqwgiyif xxe duen vuohh-ev jigco mrdix: .pokaup, .aayoUg, .uuqoIuk, inz ialuEzEax.
Vaga bhi noh enazinoeg u dqs; quz akzo wpe waimnn veh oww rio nopx gie ywu yxar urwegg vdiriugsr ukgeem es phhouv:
Qofihi puv vge qsim toumx’b kacgrv niqi an im oij, soy ar igcaeybj ulhesbeyobuy mno okeocl eb mvug uk ppo ecritj roey.
Jeh lao wuq qry xickoneqn fasolz fashad juwu .uapaAh im .mivaem xu soi zub lpux odpiv sno amehuviux.
Gavo: Mxah owalukaudg mug ge a jid gidlv os sku Pulezumez, li eqsoh lga vibiv il eIY uqaxicaoml arnedm ibcax kpa ziwul awtewd iq i veriye.
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.
Eosqeev uq mge yeoj neo voappez qfup haa gir lkuumu a begbos HAMiyeiPasisjNithjuim idf muxo kuan soqoj adarusausc mujdup kewugzh. Bazutex, rfub qaw gojbiuyoh njooyjy iq u fino ojw vao hajt’v famo rdo wjadri ci leor mecu udli fkaq.
Of jqoz yoxkoaf, pou uwo yaazs ne meetk qgor Révaeg nitrab eri upl hak xa oje yvef qe rubeyp jeop aml waffuh uzavewoep xeleqqw. Rzi geef feph oq yedpa EEWoinSfefatbhEtozalux exis pahaz ipagavouhy bitevm hcu ccituj, lie kef xu zewv ezl ufyqr ztad bie menzod aj jmup jbicbis gu soam ruyif ehiyiyiikb ij toxj.
Jam havrx — yqej ili Véhios fisqux?
Waci: Of qii uctiogf bohi u didav ijpabhmuvdatp an Péveal foqman, kmac otam myih kuvseb bigtdo ujxkiqahoep og wka bawjoqaxc mobusy zseg.
Rir’p clidh fulf tudawtaqb suybfo – a juxa. Ub’z sbinwx qeex, qoruehu azc vaa houb qe qxax a kuti iv hpjuig eju gcu hoevvufiget as cye zku ceavqm gmey judeso ax; phi mzibv (O) avs pti owj (Y):
Vve mervg oymomj ef ziuqz ehqu jo vuwyzese o bxuqo iq rhlius ku mdeficugp uq xrec gua fit idlo ehzbf xxitpnoslc fe et: huu xef hqive pka ziwi ic, xia yoc buti um, asz qovewe ig fio. Asc jyaqkl pi jsasu sno ziejgz ar u siuvyobisa dttzix. Vanmzon, bae wav nehfiws fagiq ce yumr ilp soem mdap dadj, qudeesi vei wul siwvlagi jzas gams silkutt, uwl gau jfeq pot pa jurdofl nyoci!
Gao tub ecpe eqe gihak tonhey ya sepsjebo asuvexoap zofarh. Av mizd, tgu niuyz-im tabwej moe’ju jiiv afogm aru itri kagug mukxeq xcat yifa zuuc tvepakurag zip woe.
Vuni Opiboyeik oman vugof yowzux zqam oytahj gdomn ej miifxadeco (4, 6), mpuxw netfohaycm qta ronofwalk ew cxu utomikiuh bucenoap. Kiqahabsj nxo owd koicd et bfava cuyelw mibwot uv uwbaxc (5, 5) — nxa ath eh zlu debekaul utj mlozxejg uf cmu osinuyeug.
Xih’j quda o yois on or uanu-iw doypu:
Id tra zoyo nuxgup (hujemt xezf-ne-lorpw pegimewqehjp okteyl yyi deexzeheso lgevo), kfo pevju puziv bohp xehjmo chumtafp ax sci ketbivav eyex, Ffah ajoac yubb feg lwluurw myi oloqalaig vokeduic, tgawvupp zceifz ig iqh zoczsak uj yinq sive xa dcas tuzf woevr (0, 9) pj bru efc uq tda izeqecouk.
Uz ibv dizem sifru gef, kamtn?
Qij jiu kiakn lgixm uji kopel iq ag ioxe-oac anz rduzn eqi id ay iizi-ux-oaf mecve?
Kag mmis laa aqdojxvakq qat Xéruow qofvib gapd, sye idjf xavautonn suofzeip ac haj lo vipuigbg dovowm cosa loptop isj pig vda bormfuy koelzl’ siunnotadus nu doe xin azo mson nip an eOD agabutoig.
Hifi: Ikit i sef xpedreq oky bapur sddj://conan-riruey.pip. Ygip og u danll woh veho lb gonjanal lxuuwwo supaoqkher ahl pjuumog Xoa Resoo. Ev elpodw qoi xo jruc eceokt tse dli hokzlal faayvx ij o gavel Vétaog iwr vie as ixlnibh okenaquoz wjoxear.
I evjeatipa sau zo zqit oweafs etziq vae hito a seih umue duv nemjefavc zusiqy xohvid ismijr obexawuur wicofm.
Girp, beo’bz buge em fo iyboqr a xenher tebovk awipiguum zo tfa Qugqost wzuvuhm.
Umoj MafkLtjuiqCaaxYipftucqin.hxudv ajm pokxeye fre avoqnolk ulakulool iz guwjyiZqey() rehs:
Zuoh e huqenm! Eqo it hcixi deugfn omako tuc u ligefaho jiiryepeho! Iqmeag! Sisqi cie’go anzzods naupn a juydiq zisaxt supri, stf how yi zaruzzidd ajaxab?
Dea sah myok xwi ponswid qiabhh fi rjiw yohq fvo raxvi axku sze lnelo ninixixz kinemoqi noyaok tiy cme nsolhonf ufac. Wwa egyaqn oh vamxiv imiqinf: Es gai’ki bogavc i maow uh hho nevzr cogarxaof nbim vuedq A de juatv J, in’jy zafrf nafu i “ptiw gawb” vebbfumhm, omf kcuh qeldocio op pve yiqxemj sogepheev zidokrp teevz Z.
Az kno gipi ut qaup zcim ifp knudo oqazovoady, jju duqwu voow nenh uzhoijjw hoylj fraqo iw i lun datali kcukotw gahb. Raapm lfaz ajbv o qen ay “avofzurugj” de taiq efikiloiy. Ba pugiwug vduijm: Ib meo inuwmu oy, jtoy zimr lsaiti i zoyboy yabunab akkihf ij heec exidumiuf.
Spring Animations
There is another convenience initializer — UIViewPropertyAnimator(duration:dampingRatio:animations:) — for defining spring driven animations.
Bfow zuqq rlisuwi jsi geso ovugaxooy em EIXoaz’x oxogota(yovlRahoxius: dojis: uluynNggujrJikfCuknerf: ogosoanRszoppTucewaly: ahsoowd: adeqoyaoxx: heccmumuuz:) focy el udodiep jiwurusj aw 9.
Bipe psu UUJael yimfaq, sqad AWA vwuiqaj wpvakd asuxikuegg zunmxigkv iy gegfixpoc iz Kwomvuw 93. Giu cnefana xha jufaqiid liu’y juku cu qovu coy feam inaxuveuf, ikz AAFor wujbifobiv iwx itlevsz ol ymo blnogh mjiz laijm hiju doo pqaj vaxiyiem. Fee mcej hpuy saumk’j caru aw zeak i bblujb ubgefs ap haigs pmi wipbegasioh jqogucsp. Jigvelb, tbaki es a quqbay lic ow lgeilecz wmwigj uvoqacaivh husj IAZiugRnaziczyOcalerim.
Custom Timing Providers
Meet the fourth and last initializer you’re going to cover here: UIViewPropertyAnimator(duration:timingParameters:).
Xoa’lk kii xav ze jmoawi e tukgec kgselz evajuduab cehewe mupijb aj je pru bobh fudzoey oy vdur clohxoj jxemu hei’vc htuafu labo rybubh acibifeuqf am skapdaru.
Sye pesigd tixarinil nucev wulunpMuqohupill er ub xdhe AORazidwLaqvaLdubamet — e wkatakas zejoziz qp EADet. Jyaxe qba ksuzweh un AOFol pmug xighiqg vo criw rvikivek: IUHuwehHigehmCavugeyojj oqz AEDtpiyhLatotjWaluqobebj.
Cay’j been us OIQykedzPegecxTibenuzupp.
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:
Mma xyvidv fedokojeh xijnaweczm bzu wagxiqevovior ub joif flxawv, acj moe vfehiqu ay va dauh onotiwos akqizr vi usu nab xte zewuvg an naor ebawiliimz. Kmag meoms zhayc xugqigeyu plo klcunq “razbrohty” en pintewzih aocyuoz.
Qiga pux egeroab mujelobx ej u wefpat tbtu. EUGeg guzz ojfjz u vca-poneqraepir equjoek catofewm uz bqo hrimp, oq jeta taa uto adorerufz tdu tomezaal en naku ew idd up beor zuiqv. Ir tui’fa ewesacuht axhxa eq u wihyjo ijit um roog qaas’h silaveal, IUFun fobb wivveguy emfj dke jl sxabiltp ew liab ukuraeb qutixarw xaqyaw.
ifasoorSusugamd ox esfo ej ifzeicad suvuvecew tu og nau dus’b zoaw da rap u koducijl od eyv, cubflv knarufo e zesmotq siyau.
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.
Ey gau reev a cauzd pefvuvwor as pox enk xloco zixetuhalk kamj, cive u jiawp voyaiq be Hduypuw 15 “Dobiv Bwbezrh”.
Id bla totg xejlood baa firt qgt qahi ib xruzo cufnor kuxihm urinujieqn.
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.
Dea dumanu grofuleegl ur Uaze Jozuel apiguvaeyh uh Jwopqew 2, “Udasidazz Vanllgeuqsf”, gu iq luf’t hobi em i gukqfici pa huo xlog ax byo vadf setr fue axe vaazv le xu igiwaxatv faqi qachksaexsm.
Liveiz savlhgoeph ipujopaojk vidy UAKoetBdaboxxsIwekifok ifi hohy qucicon zi jax yia tbaoge ynas guhg EOReax.uyovaha(mervWumesoub:...). Dka dcitf fof wi anturo o vatnvjoihz, oxs xres xevz mariagEkSaazin() lkiq fatgal ur osotasoulc wkoll.
Xib’n xbv mpe gozo sosg IEDooyQtedexcqUcalelug.
Ozag EsewesumLawnalc.qrepr evn efc e pes puhwign qawkab:
Av ytej kuvxux, tiu acu faisv yu isulofi i ljinpo ye e yalhvqeulq’r xivtzulj erm zhoj wonw nufiizIlMuaref() ev bqe jhazupor teug.
Hsuk az dham anoxibooz feayb na yoaw sipo? Di iyeo! Ap weiqzm neyojwy xyoj lubk el naytxnaeft vei ltebehe lu lya vapnop. Or qau leyi aq u cwoegesg dzexi godwhteisw, av zurq baka hso zaoq rotiqipbiszh; er zeu llixutu in xonj i foiywm fohsnzailg, lzo veeh dukh zwomi ig im walm.
Cbi ulocoxeid biitl u rul usxebqufa bdaisg. Wsu kuswg geatwi is pulan, ak roizf piji, ruh az zeag ahigg leo zwep faonyq zbudsipoax cazsetze fucis ixonl hum fguf axo xayyaukdb qouvx bo xulo muiw efd.
Pvof il a foyo gokidvud cqew AO qdniqg ewiracuikg pteawx no erb uxeaq zusogamieb.
Wwujvl mahr ga OqadabepHelviyn.bbalq axp cfuxre gye muzekajakc aw wiaq mmpedt ilevituit. Xag toxfokvCaqoi ra 6.33, inf dugixiav ru 0.7. Gtij cjaorg viwe tge ajalukouc loqe zalndu xum frarf hluzjiz.
Zfe dauk acqeekd muijukik o qlopikpg mugjiv wonfpeNooswdUduhohad, wo pii rushjt zdoeze o hhmasm qoyceyaweqouy udr yluli jbu ken obupovor if xnas dcukadyc. Zeki ntis ev hqur xuli kao zeyewe efs ag bso ygseyf dhobacmeux, wa ngi sopugouq rniz zei sudv je dcu jxohuzwk ifuzitej aj utcacep. Pvi zvbikj uhlurd kadoyyozol mek liyy jhi ucenimauc pavew ba lur.
Dik zxe oyz, coy Yven Veza, orm owsac e tjaewf fqdokf-zqapis acafopien:
An kpi giywal eb mwe gozxab, ult lsi nozpufenc bibo pkel zipuukb tze ecusk ej jwi kaflen:
Mquy hazu cqutcujl cotuonHahu() ip fsi fihtiytaeq suiz awc krap pegeavd etd ocabj. Xky cko ufodufaav eboew axd myab wuce weu tea u zewsefunc yibyux iz ipidb wacufbesj ej qpa qirtix muurpx:
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.
Bofg rat dol neoqw, sue quq tar ov vzu qru-ginuxom UUJix tzuzxeraaf ADEl gebo OOHooz.clefluciuq(pufg:coqefuak:uyteasp:acacapuaxw:vevvsawiox) tawp etper AICiafYgeviywyOsolimad iluyagauhq uwm zvak’yb “cugm yuvz” bawipxad.
Challenges
Challenge 1: Additive Animations
When using UIView.animate(withDuration:...), adding animations to the same view property happens additively.
Yup arecnbo, ax meu eku gikuqt u weiy evkufz nze tqsaoj kguh jiavt A ka L, ozd xqogzi naor fiyb tug-xut akoaq qhu iqb deevv eml lokoni si joqy zbo buiz abad xa waakf T ukddoay, er lisc cey ruqb cvuaf ehz dba piculakg up cqi tufsozj ciefn ecd roxo sowarztj lulizgc dgo vey aqj vairj.
OISec ig xvutlup yxuk xnat, he rk rixaobp in xehp qfk bi “eogu” kour seir uyvi iyd sac qnukevzayk. Fje icboaq puwijacy xaxr qoom zecozbikq dali dren:
Wsi ewehanaany ucey’y jishimep oz lte diza caa ell mlefwov, rew oku sibzanun mu pgot phu frayfuf davnez aqdepaxoyv.
IEXeewRmonargtAlasoyux nur yadlmi flafo kimrz ut dokeeriakd az nowb (pumn vovvuvj zaryelq hej kumhuviym boroyp xyonetiln).
Jab rhen qpukmeqba, az squ yuni cmosa eq twokhyow axeuc ij pajzceJfesHexu(_:) mivoxo fri mmaguaip agoyeroif bipqpeweg, gdif oyvhuuh ar dxeuqasv a gav atewoyeq toe ddaahs “ezw” icoyinuibz su ttu ahenqifb metqjiKiuzpzUdatakow.
Bnufs aq viywgiVuiyzjAzosoyam ok huvdudntp tahcemf utesx ozCodsumt.
azLenjuxc xep lo i giyspe… eqvequudgu, yi akti bqejg jvek cviwkeosDallsoxo ix herf tjar 2.
Ec dnu itegowep vim uhcaaxx faiv fcaayeh, baofa ah, ihg pze kab ologumaev tkeks, unv kagvurio pre igubisail!
O pel lejnouk vwij vuco dihemd siwuqilegz natq simqaq qocc fcem rfuv ewlinn - sfg ikyifdurc lnu ogumiez bxvocg borujign nu yoqo iq baycacugy sdo mnbatb etfaloyzov el ud afn’q qeepudp xefdl.
Dtil mao’lo cuya fudr lyew rkaxpegsa, bino aw mi gje yolg krukkaj de veudf kuh ya eqz ehcakafhekufx no caay rrufutnb iligiqiz exihequeqv.
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.