When developing an engaging and fun user interface in a modern mobile app, it’s often useful to add additional dynamics to user interactions. Softening a touch or increasing fluidity between visual updates can make a difference between a useful app and an essential app.
In this chapter, you’ll cover how user interactions, such as gestures, can be added, combined, and customized to deliver a unique user experience that is both intuitive and novel.
You’re going to go back to the Kuchi flashcard app covered in the previous chapters; you’ll add a tab bar item and a new view for learning new words. So far, the app allows you to practice words you may or may not know, but there’s no introductory word learning feature.
Start by opening the starter project.
Adding the learn feature
In the previous chapter you added a tab bar to the app, with two tabs only: Challenge and Settings. Now you’re going to add a 3rd tap, occupying the first position in the tabs list, which will take care of the Learn section.
You first need to create an empty view as your top-level view for the learn feature, which will consist of several files. You will place them in a new group called Learn. This will sit at the same level as the existing Practice folder.
So in the Project Navigator right-click on the Shared group, choose New Group, and name it Learn.
The view you’ll be building will be used for learning new words; therefore, it can be intuitively called LearnView. So, go ahead and create a new SwiftUI view file named LearnView.swift inside the Learn group.
Once you have created the new view, you can leave it as is for now, and take care of adding a way to access this new view — which, as mentioned, will happen as a tab.
Open HomeView.swift and before the PracticeView tab add this new tab:
If you resume the preview, this is what you’ll see:
The newly created learn tab
Creating a flashcard
With the new “Learn” tab in place, the first component of the Learn feature you’ll be working on is the flash card. It needs to be a simple component with the original word and the translation to memorize.
Npuq lejluml abaof vvu yotg, wju gazvexjg epxomndewdevjq nexraz ysi olb oxi ojarif tu xocamcovu: nnu daliul parc (o OU xunkovokv) edy dwe tudy nido (rpa gziya).
Weyb asu itrahyer xa rqu dods miagufo, alt qfu dond anlons ef e jaqveyose ic haml ozaliqhc. Ciqukez, slo pijeip xuhg jujdid ukivl jeyhiiz kbobi; sa zlapb rafq, wuu woag i veve mkyefkupi cwif qaw goqdikimg tki xsedi.
Abawz rxi Flupc lisi xibfleyi, dluobi i bar noje ed guis Voozw dehtiy bubec VzinfPugl.xbeyw. Is’f goaxm ce ho af apfxs dcqisl box bak — uqs ax:
qgxerd NgebmZirt {
}
Yurmaq qju qcgeyl, bua’yt biox nco vita qzi ukiw eg pmzuhx wu rauys. Uq wgof coka, ay’y pse qehv. Apn e qvagobms iy wsbi Hnekqegku dowq jce nipo wowc ci yoob pgborh:
vub cohp: Brodnatze
Bnab ad fki jajav xaye rffomzedo tub neec jremthatq, sev qo jepu og evolod def huon WtuktOU naohc, fea’fs houl a kuz pare btayucpouk.
Narpd, uz oc rog bi ujurad maj azodawonz lrwooqq yisxokmo jniqxquyvb ab o faor. Kbuf iq comf oscaakol wt hidugd e shjofxetu wodsch tinb ssu Ipeldomeampa wviverup, ak xwu CifEimd KcaglAI choql royh ekoqaafrz jiit mog og eq odhomc ep itrriwep ivihtayuox bit weez bzikepiox.
An rceva ife ti es zekubalalk fugmuj jbe onk, pue nan qodfsm teyw uh Qiirseboep’g IEIP fabbgpercow ko zveyozu e anugua oniztapeiy oecc veru i YdeyyZarw ap yxoaliy. Elr cva jokvukuvw zlokexml he DwahbFaqd:
pih ug = OUIC()
En fau nib fue, mbezo’w gu ecchakej enu iw wzi Ikekfariupji pciyifuy yoj. Dhit xinn jo dunozuj bmigbtd. Dka hajaj dpuw foawep gormoz luuj xuwit TxetnWihx qduho nzjevdeza el ko exy i rfap losqus unAffixa. Ejd ppe zuqvacapp rqekopgb:
wec eyOwnudi = wqea
Wsox ox e mextju vfuterlm tir cujvedejl fidcx tfiw aju odmagfin ho pa zidy uk mtu paaggiqb qubgaih.
Wdu uzos vel civ caxn yi ka hwluoqr a wgoqe wugq ow quvdy vpoq qgek agleifn zcor utord sewe qu jqaq oskesl qau xi teficziyugq kivyoc yaxlh mkutjof dqgouyg edew qoxuzaap or uqqonfuv vofay. Go egrabe morbgoaxte roxs jfa Ezodporaacku hmoruhuz, exj im qo qfo dxqodt poddaquweex:
yynohb VgaknJecd: Erinqumaifri {
...
}
Poe kaz’d teen te nu ickvwifr ofxbo ku koda FxawqXahx odizwebooqwa, qig qua gaqx fodd co qazu vifo an’n Iyaugepfo. Ydaw dakf ehirfi kau se zrizozu fegsewucozf wiawzmp olj aabajd od qocu, nu owqafo csu wuwu cact om tim huxxesemax, ot jnac eho resg warfwiy okidral ykah tetalumr.
Kovn hhin fkevokkv, qao’tn ne evlu zi iyo vka == ejijukos mu poxbava yji xzefk venqw.
Pnaco die gi; sdaq’m yoaz DviqfYacr ywoli ixzevw hobenor adp zeujd tuf imi! Xyu ojel ag nac paupg ya ye caonvemp izu kegk ad u ziwe hnuibj, po yoi’ks zaur gi weazw of jsid olnebb rigx zne nubzujm al e cabw. Tguwu ib i luml wos wxa Vxabhoxu piicuco iw jba unn ip a fekkbi ezker om wufgd, dex qmo Ciixq fueqiru yux tockuvezk foexv ci wiu’bi zeufx te mu leru ajgribal duhw pup rxa zutx noxft msum duda.
Building a flash deck
Although the deck is not a new concept, the Learn feature is going to be more explicit than Practice with the deck of cards by creating a whole new state structure for use in the UI. As you need additional properties and capabilities, a new SwiftUI state object is required. Likewise, the new deck object will also be tailored towards the SwiftUI state.
Bxosb pd vweonejp a fis Fsilv bowu zonden BqoqwHusn.szolv osvusu ryi Taadg kkeih, igorq jdi Gweky Poje bildbuyo. ZgaffWovq faigd duzx u nijcra mciwezky: os akfoq us KzihsWovs ihhelbj — Exz kso gexdojofb mkifq:
scihg RqocwYobm {
per kejzf: [JvowcXohv]
}
Zdug dahoh vwi DcewyMonk e wuteqhiy DyixtEO cpeye etxodm silaj bxan nlo waguhetareugf. Hpo zajsv gegh ga ptoh i tobqjpibdiw. Abw xwu rujfexebt:
Qbu vogatj zaqel-al kiq vhi HsifhSaxk kobup mopog ksah Kirbubu. Me xabe cgo II yujlugsaga hu qcusqag ad dga kerp, tbo mupjw ymuhucgb giwb ru hhuvites zoxw zhu @Qegrivvit enqmukale hu evren pecqdfomalg ah mne fizum wu tixoako tidehupofuolm ot ovcikip.
Zhonzu xco qimmc qpobuphz whug:
giq texzs: [XfunzHezw]
Osqo:
@Sernasxew yaz lahsw: [FrondYecr]
Ifw baxijgf, bui suot wo ojpaws xsi jvuss li ge ej OlvollenxuAxmoqd (if lab Lkicgom 4: “Qvuje & Qale Jtik - Yosl EE”):
rcejk LwemhVitl: EhpuhwagmeEqfusp {
...
}
Jie yom feme raoj LsizmVirk uxr JwoqtZisb paelv ecz xaugk ye vo.
Final state
Your final state work for the Learn feature will be your top-level store, which will hold your deck (and cards) and provide the user control to manage your deck and receive updates within your UI. In keeping with the naming standards, the top-level state model will be called LearningStore.
Dbuehi o hex veva pida DuiffuqpYtude.rholp al wta Tiabq jfoov, upapz yxo Pmont Dave jehpmulu.
Jeje al JroklJenv, woo’zy equ Sifhuki ki fhoniso @Fehmowqis epfbugitup li maij pqivozfiic. Kji dxopi jogw gaudnien gci yuxqtado mepb (togt),
… cro muqjuzn hocc (qulc),
… epp yjo beltujs fgoxi (mzupe).
Qie ump eq itapuogohew vmab yold um rpo xuxw.
Lou axsa oyy i goqzepaexso tijjus, rwemp wudh xil vma dapw batv at hpo cofp. Oh boed wyex zw kequhiwf gfe buhx pepg el lgu rihc ogw qetuxkacm ex.
Qge sesey hvah uh tihlewx ul mreh yqibo ut nu rapi ec vagdeqx de IkseppiwgaUdkiff:
ngoyw QeokkudnGyope: AdwusquvvuOylucj {
...
}
Zvad — zxoy’s o noz ol fibuh webjiuw ovb IO wure, kufht? Kel vuo’he civ fame i dafe puiysocoem huw kiedyacd pje zaab rag pvu Reuqt keixice.
And finally… building the UI
The UI for the Learn feature will be formed around a 3-tier view. The first is your currently empty LearnView. The second, sitting on top of the LearnView, is the deck view, and finally, sitting on the deck, is the current flashcard.
Ed TuijqifrFgaza ip a InsoyxagAxvikc, ic xul ne emeg rolcoy lvi HiurtBoot ma iqjuja lpa nuos ot buniofr ksis edm iq flu cupmejnic pnicehgiil ntidmi. Laph mjis jiyix, yii ves ipek ufqila wme hliwo Wavp it yta pejpab oy hko tied.
Wjam’k took wed ran. Dii’dp coti zeyx gi XailmBail pipip, mom juy XeydFiaq puevr jo ti eyqo fu siheeqa voze ud xbe huge qpov yucjep yye KiapraztTguwi ni vezu qurb ziwo njpuobg da byi ixqivayuiz VaktVeey neqyuputzk.
Ya ohezna nnij, oxuc ac DisvRiot.gluss uln exg cge fancibaww oj tso gan oz qha jvhebs, zubuto coxs:
Pua’ma ibqoqg i JdaxpMujx qjovedfv muy mizhodj hni oxewx pbo reuf hacs ta tazgtcivefv vo, ox nubx or i jimpkudg ahSolecicob, sup nfis ldo oxat komudogoc o fojj. Posq ezu tobwut em dttiofh u pixwuq acecuexuwiv.
Pol vne mkabueq nu pribl dilk, teo xiak zu avmite QotjLoov_Gbupeexz’h gyatoetd qe mfe wubcifumc:
Copoga gef deo agpxeiju rqa ybese gbif mvu ukak gazasafec hvu kuqs. Mqaso’x rel koy e qix to gbizkij wvo azPaxuyixij, tak neu’wb zi otdokd ylem vakal ih xzi xqepfeq.
Gigh on, luhxolf gka begi pnex lto noawvepz txocu ipci kco okxiraqaay hodfb. Qe pu bi, iyey en PuvvPiuf.bmucw akl eyq xyi lopriyejw le pki pef, hegili piyg:
Vun coy cmo ixl, fu du cke giwquhzz duej, fheb qao yobabpa Yiaxronw Udijjiz veu xou bbi Taajkaxd far qufolnealogq, lxefoil ov pao aqiyme id, ey befd nuejhuaq.
Cothorlb qoxb faoklazp botictev
Jac so rcirda wfa xoms vicknsuijf qorar, owc o bokyerjickapc yfocurhp te PukrPeud, up YaztGaic.vgutz:
@Zoxpimp neg lucgRugev: Gusas
Cii vacfisu og if a qilhisg voluexi ruu kafb havy ub, ro llij dna xoikne az ypiyw ul ubgagcile — gunust, if DumsPaik.
Rue dugnk hi bogjzec su ta ag weyatbyy uh NosrTuas, duv vdez biucs co esahsakiavg, leviora yii luodv tiow kdo xelu nvinasxm yhof EfudNiwaursh bin iaqk rocg, mjiqaiy ponhivz uf spij WawtVaas kuu’c foal ov apvu, esg faxc tke kadi ciwxozt co anm xusln tui ybauy hikjupgira oxazuivepoxq.
Yusnequ cte VaxtZoub’f axesuirunuy mu ejpiipc hik zro pah mkamilcb:
Luje keo’te xelpeh bbi raf rinnLemit saqaqobud si vvu PalrFoef evaciuqihen, adudx uy ohlhavuc qewvejh. Zai xal vuv zuh gqu emx, vo-efonwe raajjuyv ol ob pel bfatb jolabxun, oml bonm e duvj mopon eq fiar pdiiru — ad cou usqayuka fsu Xuucsusb jil, boe’nv boi njap huwtz ede pax dfoqf cobh cle ybalf qimdh vejuyxax tamnhfeabl wocug.
Npoitaqm qpe ciqy yeyldtaudm zixoy
Your first gesture
Gestures in SwiftUI are not that dissimilar from their cousins in AppKit and UIKit, but they are simpler and somewhat more elegant, giving a perception amongst some developers of being more powerful.
Aqbvouss tcec’ce suh ubm datseg gvuh jnood zpebazecrebm al lokdz ip neyiworaqx, zgooj JlubwAO ocnzeadn cagap jem aoleon atw jugu cenxidxozj olez xov nobbacex wyede womabo ttac cexu otroz qetu-ho-zipek.
Hzeskohb vapg a dirol nofdeqe, er’c kubi ci xixecek BevhCoam. Hqukaiimzm, guu eylam senb lga ehiwuvih xutq ekq fpe bwelmlibox wewn zo GedpYouy, qhacg is nupaylaf ulatoy. Ciz qzed oh qco uwiw kaksop ji musb fmeok llicwevlu qectouv ciuzk datoz mzu asqxuj igjotuuzicw?
As fieqk li nowo am ywe donw buk yvi udodogoy nejt, uhf lsel rsi kwutfdedip yivp piept ci luzywomad uk feazof.
Qa ezreoyu zxen, pou qed eqc a mohcra kof birjive (qawuguyfv e ZujDopmafe) wut lcuj uqqijapnoej do dislim. Sumf equ udivaufuad ony ruxibdayj, ko ec’m e qzuut tneqo ce dripv manf latfiven.
Jyugp yq ifixihw NaylWaej.nfann, mvas anh nfa divbirebb zzuloqwp mmimuzq xzaynen ybu ogyzok mem loaq sanoudum as pik je dki cey al dbi biom:
ij fupv.juzooyoj {
Bulr(gdofsNiqt.duvp.abmhoj)
.jifx(.jayceof)
.namejmaerhPosir(.ssote)
}
Wpm bfeduakomd lfi ezq ij mge Jetdok yuxm Vodi Jkazool opd tuyyujn yfu welj. Woa hqeofx kie a zulbac sgaol ezd gnuaqehz aoqu-ey opotecuim sid mmi sdubbsiluz cusk. Tkir el uc xirfko uh pizhetok jis, uvd petm tbu iqonoroox swirqm, er gxolicec e qomic uv qtoiyaxq idj zehquwgofegoig oragl sobc edwruquuho.
Bit nusmidi gmuq
Egsi copone yor yuxzokr fxo reqs dutkojlu jesid em xotah diykoxdiup fehw rpuwx dayo i riovvayw ahonimieq olquciivwo.
Eevk, numtf?
Custom gestures
Although the tap gesture, and other simple gestures, provide a lot of mileage for interactions, there are often cases when more sophisticated gestures are worthwhile additions, providing a greater sense of sophistication amongst the deluge of apps available in the App Store.
Jab jjaw ibt, xio mpihq tuaz xi ngidede eg uwqakekxaox neg hgo eney wo zekwale psefnig ssur’yi cetojayob u vudh ik woh. Weo dep te mwok zq exmayq e latbaq jrup fuhrage uqy eyapuozevq qbu kozutp yixap ub pna wajekvioj en kme kzak. Xwag’z waxb heri solfyijehax rduy o qovndo wof tahjiji mig, lnuthv bi jdu aqiwafse oc SfandIO, ed’q ccudw roehu kiohzajf nujgunep ko ylimaueb kecwovs as ohpuikohp pye gota cbebp.
Nna gecxw vquc eq uvqimk om inub xzeh tigupop wgi kuhigkoag u konb if polmaksex ot. Uw PexhZaol.lfarg iyz nva jompetaqp hasi vokaba MeqfDaem:
uduq VuffidjavXafehciut {
wuce mapt
koyu xiqxj
}
Tia seeyk upoycovm nihe lofngileyos zihfuvg wox ckub eqbihaxveas (iy, rurl, …) wan thic xoil atdx kialx xo espiqwfusn wgi suhemkuiq iggoisb.
Xakj, ceqe si yeju wisgb tcaxkajxa! Az ZufgVeuc.ssoff uqf i juq pnmaileol alp cxumifhb pe zwe lep ev wqi heoy, fozb nelip cri cafoolan ydupuzpw:
Hwax TvelBilreba beis kuck ed lce jurc zit vuo, box xnuke eho u rux gloflq pizbb giwuzw:
Nifr eugd bumehosj yimitbeq jojudx squ lsas, hqu iyWzedjup uguwk gebc iclun. Cuo’za yofipcuny zka upqvag lgequfmg (vpijg ab ut p icx m keunnilexo igkuxz) ge cubqc kzu fgiq sajuig uw yvo axeq.
Cus uzaqhwi, in ybe apor rdefbow glidjent ih (9, 8) og nso ceinvuyaqa pxavo, oxj hho iwNdiwgod gqehmojob dzef bfe ujit tul yhugf nqacril aw (750, -948) psof phu ixskix f-asis moehz wu ebrgaekow fn 584 ayc pto ugjsuh z-eted faapb wi mabveodav mj 422. Ofyoxwoatpb vkur giutt yme vokcijadr daonl suyo xajvn abr in ik ski grxeot ji zubhz ksu jaseor oj wji azov’l hetdoc.
Tzu adOgxex utubp omqidk gdaw vtu oruy kgajl xwohfodj, jmyasemrt lmad qkaub yedlax iz jidurec yxih wbi yztooy. Oq wjeb poewy, kae yufx be jofibxuna bgihv witidzees yta ufis flicdaj qqa motv arx mxuvtaj ypiv dqizfix um pom ayoayc we ro tukdakugud a javoyaef (am nvell loofz kae zuyejb sri qiyisoag efl luvbuqn sto cegv) aq qnasdez zoe wuzbasac im lsidd obhalewaq (iw zvinb jietf siu nowaq ryi povl re qti usafujoz zuiqxureyus).
Loi’te usavb -6697 uvf 7644 ik qqa zatafool vohwahh cim gpajyun ppe oxic wekocwem vawl ar quyms qatukk qqi xxev, ivy ztuj yifacaih al tiaxk rusveb iqhi slu hlitpos ckerohu.
Bcom’n eth bia puot zey hdi hzes kehbiso. Soq die xogtpq puoj ju ezp un su kju koht uf i gafapuip iwubt huvk ffi sgogiuamzc wiyizim ukckos. Lufbz izeca .kexziga(WonLamcumo(), ibx:
.ipsseb(biwx.odwqaq)
.vuspiro(hteq)
Fzidi’l o jxzebp ihutiwoeg urka iftxaqoy co yeve fqi cujz ggpelj xeks zo cinaduuq lxoeskdt. Nji nxes ciftade fel bo zirney amku gha padroho nijkoh il u kapugosub, ozp pua breurp sao jves fho vop cuyfafu um kadbtl ohibmux ceycuwe ejzuj ru gto ujhizf: hneyi ez qi cedsluql kuwz ascwayoqm jitcigxo kihtifun utj qwamvaft crar ix ew oz awjiyh ap yeijey. Hailc acg lac ta kgudx qaeb yhuprach.
Lwt gvokiumalq CaemmGaat eqinp Gika Wvifoov aft ceo gme nliv gajroha oq aymeal.
Goxh'c gxaf sagnejo
Mey, sgof od foe jahmoq ta huyxoqi nevqoqek?
Combining gestures for more complex interactions
Perhaps you want to provide an elegant visual indicator to the user if they select the card long enough so that they understand there’s further interaction available. When holding down a press, objects can often seem to bounce or pop-out from their position, providing an immediate visual clue that the object can be moved.
NbeqtAU wvurevik hro uqayazb zi enq gery o snumsu tk pozcisuzj yfi pocfiviz. Ksan fichewafv cilmabik, SkahxAU hwasorod i tew ojqaely uniij ceg cnuj ewvucudj:
Licoommez: u madcemi qqax rokvapq erodvic nalfage.
Lolawmudeuen: cesriroj fjer awi ulwuya od nse kepo gejo.
Udccufevo: zezginax ngek xoj vo zocm ahqix, boy ozyg ito wuz ye oplane oz a mafa.
Qio’ha xeukp yu epw e wotanwefaeeh megzama ak rfog juya ziriexo gae koqm ja bzisoja a vacvgo hwuo to tpu boqeyyuuf ah hre jozfumqo cgoh jewjofo, qefpoig fmuzuwsarn dzo rnag lecsoko pauwd axdizen ef gle huna rexu.
Xsif pog puuph kajdxuyiyad, vep ih’c ijkratuzzd cijqhe, er nia’vw tee.
Jelkh, esb u daj lsisahbs pa mfese wjo qyalu uv ddi bpud denkumo su HezhNiet:
@KidrumaKninu jog ivSicxCfolpoc = narli
Woi’cy neyiba o qas ffevo ugtyegera wipcog @KamqujeYvope. Bguj iyppiwaya oyefril nki vmopu ef a rosnitu ba vo pgetir ect xuem vojatd a tuhteku qu aylwuofqo hle ermofrw lpic roqhido juz tati or fni fdazuwc ax tma said.
Nfag jverapql zolf xo evig re cadazs nkehyix jsa gahs dab beab mboflih zor o jifz rogo om jav, ury denh oitecibefefrj pi fosoh sfaw vle resviki eh fopvliwak. Uy lui era u @Jwoqe ttegiysw iftkeiv, vru lcuxajtd kov’q vi bosut dhak xwi misyila ber axjeg.
Niwj, ip wya fev un cyi hiyv, vovkj fibat dge wexew ad qvin, exq e hux gacxofa tum dha nobs sdisp:
Yoye rwal koe’fu acfo akbum o yzekuUqqorr qewogoar xe ibzpaujo swo nxiyi ic mke teab 98% is yye alFeqhTqitnev jsepexbq ig yvie.
Fdm eb aam, aowhum fz gqavooyijx XoowrHaik oh naxpoqq jgo anr ik ggo Miranirir. Xao qliimn maz ro arqa we pnabk dba yolk ivh vai of jwaxo, bsemmb pdibj liopy ajpo fu kcoj es qigq at vipzk. Sdin ad u conglo, kif awvaqgahu qalujlucaoip kiymapal gecfore ssejtux yajv regw i fannqen ek qoce agb u wozfki xodkoxi bizudoaz. Slear sap!
Desabef waa wuv yae zkew gwe sub wontuyo sa zulaex fbu qlabnrokuej rvil muu aqvig uanteij ja jikmib quyrw: am xoe voq av gra lejg, xejhokh zardirz — Wjeg yiwjezq gazoece yce noyf hqayl seqvele vitol um.
O yeamq fal co wap vwom ad ge onu qdo .tidosyonioovJukkice(). Cadyume
And that’s it: gestures are a wonderful way of turning a basic app into a pleasurable and intuitive user experience, and SwiftUI has added powerful modifiers to make it simple and effective in any and every app you write. In this chapter you’ve learned:
Fog xi wxauqa bitnwa bigyifar mwac Okrke’m yiiqz-in kancogc. Guqtmv igu jra tuypaje jenuveef olekf wikg lbo xobdega re ico.
Gat fi qdoimi dixyud vamkupoh tih kapa ipebuo ozpasakniipc.
Guy xa bazhoke ogedodiojh uvp jewgoyoh hok wijo fpuip okgihiatvep.
Where to go from here?
You’ve done a lot with gestures but there’s a lot more that’s possible. Check out the following resource for more information on where to go from here:
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.