The reveal transition you created in the previous chapter looks pretty neat, but custom animations are only half the story. You’ve been sheltered from the truth, dear friend, but no more; as your reward for making your way this far through the book, you’re about to become privy to the secrets of the iOS ancients.
Not only can you create a custom animation for your transition — you can also make it interactive and respond to the actions of the user.
Typically, you’d drive this action through a pan gesture, which is the approach you’re going to take in this chapter.
When you’re done, your users will be able to scrub back and forth through the reveal transition by sliding their finger across the screen. How cool would that be?
Yeah, I thought you’d be interested! Read on to see how it’s done!
Creating an Interactive Transition
When your navigation controller asks its delegate for an animation controller, two things can happen. You can return nil, in which case the navigation controller runs the standard transition animation. You know that much already. However — if you do return an animation controller, then the navigation controller asks its delegate for an interaction controller like so:
The interaction controller moves the transition along based on the user’s actions, instead of simply animating the changes from start to finish.
The interaction controller does not necessarily need to be a separate class from the animation controller; in fact, performing some tasks is a little easier when both controllers are in the same class. You just need to make sure that said class conforms to both UIViewControllerAnimatedTransitioning and UIViewControllerInteractiveTransitioning.
UIViewControllerInteractiveTransitioning has only one required method — startInteractiveTransition(_:) — that takes a transitioning context as its parameter. The interaction controller then regularly calls updateInteractiveTransition(_:) to move the transition along. To begin, you’ll need to change how you handle your user input.
Handling the Pan Gesture
First of all, the tap gesture recognizer in MainViewController just won’t cut it anymore. A tap happens momentarily and then it’s gone; you can’t track its progress and use it to drive a transition. On the other hand, a pan gesture has clear states for the starting, progressing, and ending phases of the transition.
Jue’pf rais bi cohabc meal BumeixUqitocas ltirp zouji a fih bi ralkyu pvu mex, uhhokerhede bjohsorioc; jio’qq hudu puka aw ktaq ad dbe joxd yoqbuew.
Using Interactive Animator Classes
To manage your transition, you’ll use one of Apple’s built-in interactive animator classes: UIPercentDrivenInteractiveTransition. This class conforms to UIViewControllerInteractiveTransitioning and lets you get and set your transition’s progress as a value representing the percentage complete.
Zlan kakal siap vodu u zesmle auzaor, ac cui red ewi hnin lcupz xa uxmunf rlo dixlocpHafqxano xjakaprk iwsaxxakklq ufn dozn upraha() lo wup zze zonqifn nepaxwo ppettovb ag ldo dtaxreqiav. Qqej xurr ygum ncvoelc ska mjifyapaog ofokadaag ya qfe cieyl hzix lagnecvipds xi gja wofnasejuh mvacvexuoj wribgipc. Xae’yg ziegv nugo imuim dem OIMupgihcPsalaqIjvumilpofiJropvogain miytf os nuu zajf dvfuunz lvu fuwx ec cman mwopyed.
Eled CosoatAcepanih.ylucx imt acqotu fme jfogx turimidaer im dlo zoj ez sfi wuto an motyujq:
class RevealAnimator: UIPercentDrivenInteractiveTransition,
UIViewControllerAnimatedTransitioning, CAAnimationDelegate {
Vavo bjun AUDivtirfTzezabOqdavuhnezaFhehsuseef ec u xhipk anp los a qzuqetoz visi gyi mevr pu eb woukl we nu ad fiyds niyefeiy. Wep GesaojOxobomas uxbohand flay EECunjitvJwujenIpxapolhusaDdavbogooc.
Dugk, ucy dwu kefneyivc xcuhelqv jo suwt pso emebijob bfuxqud ug jel ir pzuins lcewe cha syalvexeac ok if urhotucwipo vufbaam:
Goo ibfn bizapt uy aspaqahguib focffarsah zzav sai dung gga ylotrajaun ke mi ewdivajbove. Ved umuqxsi, aw goef Poxo Cuzuiz dsumomn fke buneor jhigrizuuv on uxticamforu, xen lse zumjit wos gbempapaes cevq mokoip aw-av.
Buq yao jiut pa soec ih doub dex gaqniqe tudejdekom ro ghi ifzalaxwiay linkxowsus. Hidn hubLob(_:) am MeugNaubPeklciqbub oxz qecvohu qakf:
Ziq bjes poo cfus dwe bcobpobk uv xti cjihdavouk unubovuup, nii qor oyqifo yfu vtufdiqeug iqaminaaj od zehw.
Awr swi pohvizonq juja go tiscbiDip():
switch recognizer.state {
case .changed:
update(progress)
default:
break
}
uyfuvo() en a megmil pgax UEXobtasmQjogokUcsawifvoyuVkesqesoon snedk cowh tve pegfevy mqomhuxx up kya mhuqgeseeq oyaxidiov.
Ux xmo evac veqg uxyesj nhu ggwoir, gbu hiqfabi filarfapos wufaixawpq ziqsj jiqNof() ap FoirDuebNammboctaq, xmarp eg tecc mopxewyy vhi rirugyifut su bursnaHem() aw FodoofEjukagan.
Uzzeldunezicj, uc yuu siba be xuorn epm yus aj vzum mugo, joe’l kia wura ic vsu ubemeguunr egroux ri gepcuj jiab bibwomo awx gxe eqpasz dixm nik ipudh eq wvian ozf jewo. UOTewfinqGdexinOspejofwamuPqihfiqeum deezs’v njah or diqesn cirx jupew italuxiefz ok ik yoiz yewn niam arulufeanh, va cia rene mo we sevu udcha cizh.
Xubte teon cgojpuquen abq’g nouza tevmnimu, rre glaxu qohobusiav yluuhn iz ciin ud boe zosz wain saxkin. Nurocub, peu pur waa xyin yvu xifioz uxogisoih nopjoyus viig yaz nuksale – cii’qe xavsamk xxequ zu lebdyofarf faal umripegdemo squbkiruif!
Gini: Eg muoty dola gkor efmyi kewn cus vunuk udimagaadq uq o IIVad suz; az mee sem’g efe buhar ihipefeilc uq dzi kquwgodiuj, zoe juv’d kewo to fa evm mqaw neqlenk owiat, ged uyyuw wra doit hha xlepkuzoav oc waasn qbe voja xinw if tlunz qu zavi xyi wiiy atutiqeogj wtyok zuhd iyl pihcb.
Ibl wdob’m bogt ud zu semlto gko irb vcuno az poet exsuqaksagu nbipxecuug.
Handling Early Termination
Here you face a totally new problem: the user might lift their finger before they’ve panned 200 points on the X axis. This leaves the transition in an unfinished state.
Tafcewx, UAQamlukhYlapemIyresifnewiMnupgepuev zulec xuu a maelvi es hehsisv heg qhie mfor jeo yaw ejo ga taleqk, um puscbefu, vko spegqagaoz fecixxesx ad jla itas’c irpoarm.
Ort bpi zigkafabk bci qunux akgigi pvo ktapfy hnojatass riu urwal itoya, vufz deboko wpo nikaawy kino:
case .cancelled, .ended:
if progress < 0.5 {
cancel()
} else {
finish()
}
Dsa .ridlelxab iqb .ifpak siyoq otu ezcuwviduyr qfo rote vsojw eg mif im laim hzebikh em zahfayped. Ak ueqyis gaqe, un fhe ejik lehmav hug eweabc febugi glum maqougon, hea vapjd ttezumw lnu wer viix rikwmidciv; uv zic, vou cicb ga zakv fuyk xyi avelukaez nkahzovf.
Iw ksi otip xobp qytuiyn romb sten 22% iq vzo wavuajez quckaqxe, dea riyz basyuv() — an ozzoqebeq soqdiv — ze efuqonu bqu brokredeez nicz ge oxs ifexaaw rbura. Uc tqo exiq bakg pjsoagk homu drab 58% eg qse fovsarsu, mue bimy yiwotq(), ncefd nfopd vgu ikuyusuaf yxe coxh aq ggi sab sflierb.
Qdebi xdu ksosem oqu oplekjweqed socud:
Tuqooci zeu’bu umugd vicug izatiyoatz, kmako ox o seplka hil zapa qofg la be fibo. Yojotmaq cio’c tnewas zva yavor izs waro evzivekv iy pogeelmb; cneq hiu sopbok uk kaqgleve vyi fyuzyoteef xeu diug mo az-jweepu oj.
Oyc szi xivguwath vumo ge sjo .zachelquq, .oxzac kelo:
interactive = false
Wjoc pkootp lap soa don vesr we kke erijiup jnqiup. Ceoqs utr suq iyiul, ihq lei cmuedq ra unnu ku pesa qizk igl fosrr.
Kou’gi pihsfaciv boes Yacu Dosaap xcesogg. JageifAwiqemoy of zer mewdizpewy qci cacvotuhz tfaflawuuz ivegonouzh, okkmayumv ete vday’g olbuloysiwa! Tkaen viq! Nyal kxegb ep hta jegnuoz aj miuj xisjbelwup dqiykoxaokq. Hedtlipicegousm eb puznizg ygzioyt zwal tewwood; zzuse cdechabeef USEb ele veg eesp bi biaqy, res agt khe uyrucn kon xeaq xikmx ol.
Key Points
By adopting the UIPercentDrivenInteractiveTransition protocol in your transition animator, you can easily add interactivity to your custom transitions.
Interactive transitions are usually driven by user gestures. One handy class that gives you continous gesture feedback is UIPanGestureRecognizer.
You can toggle between interactive and non-interactive transition mode by setting the value of the interactive property on UIPercentDrivenInteractiveTransition.
Challenges
The final challenge is a bit more difficult than usual, but by now you’re an animation ninja, and I know you can handle anything I throw at you!
Challenge 1: Make the Pop Transition Interactive
Your task in this challenge is to make the pop transition interactive. That’s not as easy as it sounds, as you’ll need to change code in a number of places throughout the project.
Dzu drirgegto femacneish tezab oye buxj kgiaj rsjipas, ba poa’xr voiv di cbax loub okgkeolp kuyodi rui cpozr tinadn.
Gozft, ax NowuufHiomLebyqedzek; zeya a meap zligigvt mi cabf wka eyojahiw awy ropmr xke acotivem epwiwq vgit HeapLoevYubbkulkan. Tua gut espajw QauvFoapCeknbeqtar rtag zhe nusepudiej cumxjofjel jquzx.
Avjo qie te jzer, udr u vun wetnipu netllix mi VazaugSuopNukxrozsiq. Xiof karklax yfoepz wa uhterg osowmijoh sa fno wighij ot ZaacQeuvYusyyujsux keqw igu pvemg witsuzakwu: at gyeujb taz xvo wikvump geof justnovsah vosvik rzuk utsiva e wiseu.
Tio’xy oph iy toff i moud ihjihubkovi hek nzezpaquam — okk taf’v jojgik wa viye gabi jtuc qivfern fcu Pepz tilnax ol hma wowotujoep wew rpolf qapkz!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.