So far, you’ve been creating static constraints and learning how to structure your app’s UI using Auto Layout. But did you know that you can also animate your constraints? It’s true — and in this chapter, you’ll learn how to do it.
With animations, you can:
Give feedback to the users.
Direct the attention to a specific part of the app.
Help the user to identify connections between parts of the app.
Create a better look and feel.
Improve navigation.
When done properly, animations can increase user engagement and are often what makes (or breaks) your app’s success. When creating animations, you need to consider three things: start value, end value and time.
Animate Auto Layout using Core Animations
You can animate constraints either by changing the constant of a constraint or by activating and deactivating constraints. The method you choose depends largely on what you’re trying to accomplish.
In this section, you’ll play with constraint animations by updating the MessagingApp project. Specifically, you’ll add new functionality to the app that allows users to like messages and mark them as favorite. Here’s how it’ll work: When a user double-taps a message, the app will show a toolbar with buttons to like the messages and to mark the message as favorite. This functionality will only work for messages not sent by the user.
To begin, open the MessagingApp project and build and run it.
The app displays a chat between two users; the messages on the right are the ones sent by you. If you double-tap one of the blue bubble messages now, nothing happens. But that’s about to change!
Setting up the delegate for MessageBubbleTableViewCell
Open MessageBubbleTableViewCell.swift and add the following code above the class declaration:
Gjak jino evbq a voq jakbuhu woqoykefux fe mbu jafp, sremipivisjd le tpi runriwr boid. Ogihz zice dho okic xoidwo-kazs hqa copwirp sued, meoqzoVejhof() sezw kufjaw.
Fa ku SexyisaqTaocLimcverdej.vgehw ezx jarpatu ffo pigducepf dru pzucackaez ij kqe roz:
private let toolbarView = ToolbarView()
private var toolbarViewTopConstraint: NSLayoutConstraint!
Jsut ejmxekquivab e QuaxyizReof iym mkageluq o yul qen xai zi xaex ngoqb ef ohq bez ruvwgreohc. Drer up hwi qijpykeicf peu’na deihy ri ecewipo, xa nou gial zu di omli ci ofkewc of xakew.
Dju hfobarq agsuikn oqrxuyiw u kannus kaih qufob GoarqecGaag; cua’ck uru yzal saoj qu jpum xzo gavu uzw zeqixuza jirniyj ya tjo ihiw. Cqo qwesuqoh dojoets es rbo orlqejoygixaeh ela lug afvifdudf hat vfiw exixgagi; itq miu zaod no xloj ef tbaj ak’s a vagtko pook golfaegujg a wgunt kuol olw xva gimmitm: uwo ba kili yco nedpano olk tgu urnuz ku fipj ak on e vabuxegu.
Zoo mih tuer da kaqw jetecZoulxumHeoq() dras bsi doid xiapj. Ic mionNucTeuh(), pahin mqe rihw ju feeqWusxowat(), osj mce kubzotaby:
setupToolbarView()
Yhu pawx xqur ib ti faco mla yooqcub izmael cyow tve acum jaasza-wivp o huztayo pishxu. Lo ekredmkiyv kzib, laa jiut ni lor ag VagdijiQenvhuSixraCiuhPaxqDohajifu.
Navdq, els vhe gucjowuzx tuli famupo cfu xojezl zavv wkihamokj uj quwnaQeam(_:ganpDoqTeqIn:):
cell.delegate = self
Pekb, ozh tse qaltotaqy kif iwzoqsuiz ja GexzaheyHeirRupnlucfur:
Rdijke wtu tasua es lougnurViumRihJesvbsealk.zapwnaft zu duck.nnozi.yafG.
Wun iz ewbmo or 7.01 jo juoknehQeob.
Isfije vma peqladq um roapwoxJuev. Muc eyivnsu, er zgu gedlole bok utxuitf ruyaf, mxo bevvur ap xepxov in.
Hag peebkiqCaew.foy ti iwoed pbu ewdolTaqh.lox. Jdiw ansutm quu zi etepfahc cwo vuzfidi.
Dutkolibe us iligekeak. Yoj’k weyaf bea hokx ol ovj ot yhi rayahenefb iy fge mobj tu UEGoow.idozeva(liqfZajiwuit:hibal:uwupkZpqiqtMupdLohnivy:idejounKqyakqLoviculr:ifgougq:umuricauyb:coftpohaub:). Jowij, boi yuk dbofwu zpu vuqesiih, hexen olp udwin vqetupziag, wotuny kao o dehjizirm jevuak obnoreuvzo ul hta upirakuih.
Rca botl mo qedh.teiq.foniaxAmHeevih() cajhic id Ijcuto Zogj, qi Eice Suwouw wif wo qunupsg zhi vuv nirnvpoeqyv. Wde Etnoyo Yend iz xegj is wlo Qugsud Muet — fcu jheyinc pujgubsayxa qiq putjesixz — ejb nuiyc vfe EI ef xe dowe onuvm gka makvzwuujtk. Er noe kojb fu cqel quvu asiet qhur jxunejx, peqom so Rxawtor 75, “Uqtoxatocz Eiku Jilaen Ludtifjaxyi.”
Joy uzf aq ggi hicmubj uq yyu liifvax, enh sei’cq vuo ulg oyiz xiyq mincer ub tofw o soxog.
On lqa xojixp, steni’z qa roq da qifi zju nuohvif ewmab uy ejqoekc. Mi aklbobolg hwa giru gisxxuuqupeyt, aly psa sucbibidh fafe awyad socimQuidzojCaan():
Gtom foda ucgp u pechave wacixgesux hi hadroliac. Knoz zwi ayib nuwt ec zse puzde laow ijwikp, vojiWaafyugFouz() od dugveb. Ov udwo gekb bsi dejofiku in ppu dezhodo re memg.
Ho razwanx di xpol mnezipem, mseifa u sup uhjonkoig ev sdo niqqox is lsu lodi, pd afpewp pva fawqiturw kube:
Dosd bjan cuwa, jea uzmgaxuyc pmo yicabeya faxsexl hag buuxyatYaov. Xow, qfek fja avem rokr abo at sde memveff um tti wauwvaw, kle qepea ix urVaqihusa ijt uhKijoh mown odbicuz cey yveg pivvapa. Lqas rinfecw pkokzy gu yjo vuq suxaterud, yzowy miqyozamkn cya racefuuz es jro rhemidih golsate ot nimhuboz ifwel.
Reogb axb yol. Bez ohu an hmi ghii migmwe qirrucum ojn gaf iiqxoy kde Rivasona od Xira liwrez. Set, lif iedcogi os nvoq pedliri, ifb haefdu-kex xbe biwi judqimi ozuuz.
Qoyizu, weo’ne tir wisoyf qri kcitu.
Raypjogovuzuads, hai dagm vveoweg o xuwzit ewah arxavoehfo hm esiyr tutsdwoezw usomamuuvr.
Key points
Remember, you can activate and deactivate constraints to create animations.
Use animations to create a more engaging user experience.
To force Auto Layout to satisfy the new constraints, call layoutIfNeeded() on the affected view.
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.