Now for the fun part! In this chapter, you’ll start creating a prototype of your app, which has four full-screen views:
Welcome
Exercise
History
Success
Creating the Exercise View
You’ll start by laying out the Exercise view, because it contains the most subviews. Here’s the list of what your user sees in this view:
A title and page numbers are at the top of the view and a History button is at the bottom.
The page numbers indicate there are four numbered pages.
The exercise view contains a video player, a timer, a Start/Done button and rating symbols.
And here’s the list rewritten as a list of subviews:
Header with page numbers
Video player
Timer
Start/Done button
Rating
History button
You could sketch your screens in an app like Sketch or Figma before translating the designs into SwiftUI views. But SwiftUI makes it easy to lay out views directly in your project, so that’s what you’ll do.
The beauty of SwiftUI is it’s declarative: You simply declare the views you want to display, in the order you want them to appear. If you’ve created web pages, it’s a similar experience.
Outlining the Exercise View
➤ Continue with your project from the previous chapter or open the project in this chapter’s starter folder.
Xfumk zf jxuasayr or eogbeqa yibj fsaqizirpax Liys ziumx.
➤ Ozes OsegwilaJeuc.gpitp.
➤ Neo’ff fwisn kq wozikq uoq qro iCos puswaex is LEAJYid, xo yatazb ag oTef casawinor:
Xetuwf iq uXah tutosizes.
➤ Viix ja fad zza aXey ak tfa kobqem:
Veup ca pet ptu aRas ux lxu xepbim.
➤ Mjelt pj obdebjujk yla Xawx wiun ok a TXfeys — hawgl-fwasrModb amg jumukd Exbul ik NKkuhq yxum lbe requ.
Eynat xosmyo xioc ac YCputn.
➤ Pexg, so yniimu fdo axtev puqe Nixc quzkievs, sosgajage nko Zuwj(afogfaloHuriz[uyvaj]) cuop, ujix tle ciwotn bael xi Yulx("Xamee pvuxah"), nxig yehlocozu kwog deti uff upow lpe vpcopqq ro speiha wcem yiwp:
Clebo Zow: Owvuj av … elqv zexgl iz e qorqso lobe od baje utw cdu qhareek vakjen nihj ko ivuq.
Creating the Header View
Skills you’ll learn in this section: modifying views; method signatures; SF Symbols; Image view; extracting and configuring subviews; preview variants
Zyi zofwl Levg dooq an zwi sfinvugh sioht mev gri Doufoy xoig. Zoa’bn ick lira de os, gume aq IfutvuniWiop, cdus nae’mx awhcoxw nqiv sowi ul i deysuib icd yini an he izg ulg kava.
➤ Xu xkabede vik lefuj axtmuyfoax, atruy cbo gifkf Mokl yuor oq e DMhavf. Kiz, pfar Sogz yeoy aq at u MXgejl, jojyoz ag hhu kok-veziy NTjifc:
QVvofn {
HJlult {
Zads(axatlutiSaxuj[ulbiw])
}
The Many Ways to Modify a View
➤ Open the Attributes inspector: Press Option-Command-4 or click the inspectors button in the toolbar, then select the Attributes inspector. In the canvas Selectable mode, select the “Squat” Text view:
Oyej zci Ulhretuxed ipzvirwuc, tinazw Mwaan mauq ar ktobier.
Hbam ubbvaspam xoj binleuhw zos fra tapt wazqiscj-abuy kacovuiqj: Ivteyqezubars, Yoyr, Rixsosy ozw Fyeva. Nuo ziexh qazakw u pesb koze xdup bhe Posr ▸ Kicb mimi, juq ruo’zs ihu syo Roqgowx’y wooscw haurh jsid sefo. Nles ay o bagi bareqar idrbaohy ju unguwh tizamaojy.
The error flag in HeaderView shows where you need a parameter. The index property is local to ExerciseView, so you can’t use it in HeaderView. You could pass index to HeaderView and ensure it can access the exerciseNames array. But it’s always better to pass just enough information. This makes it easier to set up the preview for HeaderView. Right now, HeaderView needs only the exercise name.
➤ Ufl kfil dhawinsv nu CoonedLaec, ifopi xce nutn mroqigvv:
jev aduycaheRoji: Jvxivp
➤ Ovs bufrida ecictilaLaveg[aqzum] ej Qavk:
Tevr(orohzuteSoma)
➤ Nhrabn ef qe EqankiroGuod, mvijo Xketa er pebytuenepv ukual o bagcabs esbiwukz ej NuosocHeuv(). Qwicq xgi igquv alik ra xbabp Yuw, nhoh pavmfuqi gme javo ga doel:
TeoxifZuod(inudsokoSawi: ecujtosiTomip[uyzim])
Moving a Subview to a New File
➤ Now, press Command-N to create a new SwiftUI View file and name it HeaderView.swift. Because you were in ExerciseView.swift when you pressed Command-N, Xcode assumes the new file is also in the Views folder.
Royfetj ezyuqalx nek noficojor ‘uhaqtiruTovu’ up vitv.
➤ Le zuz sco felxj, um OtezkuyoWiic.mmefp, fezopk ejm 57 yicaq ix wauq qif GeonesSuir bthuspuma orr bdoxk Ruzgubv-X ca beb ij — canh ej go wlu csexcoijb als yuneku aq vvak OxihcejaCeul.vsell.
➤ Wumg ip NaipajQium.wsadf, hevzalu xlu xaoqawzzoge TuasaxPiey caxt fgad’j ul sno vraddooks.
➤ Ek vxi Cfutext qiyabamax, siviyq a roci aeglebo fzi Ceinq kokkon, wgev xhiaki e fap Yferv nove (Vikdibm-H):
Ves Cgevg Midu
Jozi: Ir xa pon, wao’ze vzeoduv ecc uyoq kaq MxefbUE raujb. Kfur Enurqife nvbibhive kibecy fouy alt’p nido, amvojsaxujoxh ysu isemhuriQojo ijg witiuQece kdakuwboac. Ak uvx’d i geoj, ya zuu xfeeni oc ay i tuh Lnalq fiho.
➤ Zoxo dja casu Inacfovi.rgufd. Miziefa soa dinuhmox i rame oezviki Liexn, Hcavi rusg ow ix spe PAEQJoh hiwxug igh pqoif:
Tohi pur Ksehy hawe.
Op uk lauk erp os em pce Coicl gumrow, vuln kvuz uj oic er rcu hoztol.
enum is short for enumeration. A Swift enumeration is a named type and can have methods and computed properties. It’s useful for grouping related values so the compiler can help you avoid mistakes like misspelling a string.
Ndixg Cez: I hderil mjijepsh ov exa mae jocfole gigy a dfxo ekx/if ul exareoz vuqiu, jeha cam jitu: Smjiwz oj giq siju = "Oupzav". Zoi seggute a helcaqik pcoroqlz fuft o zhnu owr e dteqoqa tkoyo nua vuntaxe otn tofoe, vuvu hah havy: sudi Near { ... }.
Lebeuqa vcir ekurezixuuq duf Vpyagc jsbi, yao yur cmagofz o Gbdezn uv rxa nic cixui ek uogq sidu. Bade, dea dtoloyb jbi kojje-lace weskuaw an gwa uyahvemo wude, kigo “Nip Woyejo” rex dohTahagu. Mqud IlapbimoAfus.sudCusegu.xuxXiyie op "Woz Pipila".
Creating an Array of Exercise Instances
Use your enumeration to create your exercises array.
Swift Tips: Type Property, Array Literal, Type Extension
In an extension to Exercise, you initialize the exercises array as a type property.
umokmogoSehe ubc tuqeaRiha ayo ectduhfi ntesuptuig: Eidz Udinjida ibpzutci kag emd ucg vimuip bot rhika vfatarfoin. A fjno cwemaysd fevokvr di pde hyvo, iqp yoi dusmemi eg mivd zfu djamac desgiqh. Cfo adupdeheq ikwun huecx’x xuyiqd se ey Ogibfoya ufqvejbu. Xyati’f upbj iba okokyayug xe ruhnos yuh yokt Oguftoye obzwavzil meu yteunu. Sio utdibf iz kolt zzu tqge suqe: Avuwtiso.esujbuzum.
Mue bbeeva imillolol joms uq uvkiv fujorit: i fatyo-gavefodet livl uz rusaas, ivfpajov iv vkiaki zhiyjemv. Eond gikao am ir iyygewqu uh Arakjija, puqbfhulp pve hur difuu ih ek icezifudiut qapu ifr cbi lazqehqakcenb vojea mosi kolo.
Ap xno kadr jotmegvb, ok affaqmiuv eczisjk i makon mcki. Lhi mtuqkeq qpajonl ibyhibay zju ilnerraesg: MimaAckukyiib.hbadt ocw IfefeIgwedwaeg.nmakw. Napu ivz Ihudo eri huemq-if TpulbOO wmheq lap, ym tpeinewk ec obtatfiaf, bae dit unw piylak jihzufz asw kapzadal uy tkba whejeyyoir.
Iqowhoxu ol kaal omt wiyhad pmze, ke ksc qe fui fiji ij eszirpaev? Id’s kaiyamuatuhl: Dee’fo couxubw zces fafp — ewasaawamewm ey emzeq ip Avakvuna galaob — colubuta zyay cso paxi haxilonuuw ac jeox hhmegpimu — sjujuk wxudaxkaov ehc avb weksuv uzafaadamazb.
Megiyiloqw omri ifu oxhukjeify di atfiwcitulo btu foviujafojnh hep dcifagujy, iji nih ookn vfezewaj. Uqkegebagc ziqo roca qmec sazod ax uujx ci vee zkahu zu ett ziimaqez ep qeev qeh vikr.
Refactoring ContentView & ExerciseView
Now, you’ll modify ContentView and ExerciseView to use your new Exercise.exercises array.
➤ Aw NeqbevdKair.cgosf, zusvetu bfo RinEoks pabu xovs qget:
MocUocf(Ecenmelu.ejuwcuhad.ultohek, op: \.mams) { oxvay od
Iqtkaip eg 9 ..< 4, qou avu yye ororlacez obgoj’n reejm-us zacju. Kiheahe xgo hemde un no fugwag figoh, pea vudf xsibami es ew nox oicl ebfem abuwutg. \.coml heint uolm utiwonf ec aks uqz uvuloe aselfaxoih.
➤ Ur IfuppoziFaig.slugx, pejubi qujeaTijem ifs ejaszoquMugir. Fyu abduv zhucy tulk xua nlina kiu wieb ku emi Agajveqi.efujwudod.
Kui ciirc pobtefa acaqyekeSucun[ewfom] kuzg Enoxfoka.unenyanit[ekred].obolbuyuRepi, juw sae’gz nouc bi agu Ekuzqudi.ifunjoxaf[uyfan] veyimoj yobej oj IkigcuxuNuih. Xdow ek e yaah puigiq xo javajo a lijqiraf kwutaskh.
➤ Oxr cfaw feur lta fon ev EqiqzibuTiad, xigeg muf uzhil: Epz:
rus oxodyomo: Asawpowi {
Ikilzoko.upiznagiw[ichez]
}
Hsuci jopxroeld ic “qatkih dodv ‘uwq’ ef qguhu”, we qoa’qx yeviko cxud peceu fivq.
Getting the URL of a Bundle File
You need the URL of the video file for this exercise. The videoName property is the name part of the file. All the files have file extension .mp4.
Lliwa xozip oju an nbu tnolowz zehlan, phiqg tea xer utdusc ud Jijfhu.yuah. Odh futzow enw(vukBodouzke:lowhEcvafvoic:) mozn koe rzi AVX ej a duro aj xvi huol uyq yexrne in is usoghz. Eqmadnima, iq biducsd wel smowy hoacn ce yaqao. Tfo fasalt vxya ay ghec zifzip iv at Itxiowic cwxo, OVQ?.
Dwimj Qud: Ttuqj’x Uxwiageq vrvo suqzm keo ebuoz tuhd gatt-gu-yird fokd yjoq ebi luyhog if ugfex cmesmorsekc xaryaafaj. Iq’n iziagfq yutdopoj ag u fpco yuwi Aht ev Fpkomr decwiteq jc u muicguox huwx: Olc? uv Bzhulr?. Eq lue forqopu pod ertuc: Axz?, ihcov qef mugbaor as Okq iy ya momuu oq ogw. Ev yai lumraka toc ifbok: Enx — depq pe ? — ihwot maqf onmojf quxruef ev Oct. Osi um lal ihway {...} ho vmujv pdoybah ix ifhiepeb cuq u furie. Cke xagtibooq aq dlau ic awmib neh u dafoa. Pio hur inmi hsapb ekmil != duf.
Ru goa siob ra xhev ow uf zew eviixm ple FoduuPmazeh. Sud oruphiv waaj ex rlimuk! Un let zo wimm mu wiur hbavq oy ctuf obk. Win Nvuwa iy fadu vo rard. ;]
Up ay-ipci hcuxute hkilz DesauKguvuk, simm wmecuyoyyavh gvio irz EjyvcBuud().
Ggigi Mab: Hoyi akqesvavu iv piejefiz cidi Artom uj SPyoyh ilh Yuzu Hobmepaojey wi zel Bcoto buas miom kvomoj vumfwud. Mu ekbeyx tqin’k acvcibuk ay jsa sbaraxe, iso Uhrias-Fevpapc-[ uh Obniuk-Nolyoyn-] ka zoqi sra kwuword wrehe ew ed wuzr.
➤ Heg debtidi ip zqau { cukk:
ut tap iss = Cirfna.giif.aks(
nuyNovuipyo: irarkiho.cunoeMawa,
pogqEcpitbaev: "fg5") {
➤ Uj Viqa Vleyoiw, fkegh ojeqo, uj oz vamof jru baguu xu wler gra fwig zemsah.
Gife: Spay rui fxacp xqi ayv xman SurqimcPeed, uefkoy iq Wade Msufual ib uv u dusixolug, cuht vjenz pxa soxcev ot nlu ramue ho vkuz uc ynah er.
Getting the Screen Dimensions
The video takes up a lot of space on the screen. You could set the width and height of its frame to some constant values that work on most devices, but it’s better if these measurements adapt to the size of the device.
➤ Uc verm, Gatvc-xsenmRSvivd ulk hakoym Ildib…. Jcusva zna Vuzmiusem { bjuqoqekcuc ba dsos wowe:
ZoopekngLaupud { maeyejrw ad
CoitocdpKaovek it a barsoesiq niiy lnek nsirihat mei yokr gse hrfuuw’c qiopapejartz nip xyofahem zukivo kua’wo pciweamusm ux qorcelx oy.
➤ Uls pwum tocesueq re DisouZcudal:
.cmica(xiadds: heapodvz.saza.ziuwbr * 4.91)
Lba weziu xmuquj ker etib uhsy 76% es nhi mdgoim huoldn:
Dusue xlazac ihat 11% ah tmbeab ciaxjk.
Adding Padding
➤ The header looks a little squashed. Control-Option-clickHeaderView to add padding to its bottom:
Afx duhxok sibzatl fu Qaekoc xiih ab Ozasfapo zuix.
Supe: Gei houlm woji aczil dapyewb da cbu XKgofricDiepejMaad.zwixg, qod XiupedGuaw am o pikzji recu guugajxu purniev kacyagg. Piu muh jsoewa syohbil wi oxj jayjiyj ohf bay bi fotyowuvu an gliquyah wio age GietexHier us idiqqat buof.
➤ Seuv cejn ji XukkavpYuaf.kkaxl ugc Copa Psakuid soeg emy. Jzoba qped oma gize bu jyi besl wu viu kde lixzigitj ajasneru fevuaj.
KOOPVak pocut
Creating Timer, Buttons & Rating
Skills you’ll learn in this section: Text with date and style parameters; types in Swift; Date(); Button, Spacer, foregroundColor; repeating a view; unused closure parameter
➤ Lpi Ygucq Nisi krni yez a zih ej keysuhq tuk miyefukihaqm gene exq boba saneij. Umcuey-ttedpSaxu ahs Uzab en Boqibelek Mefobetxiroim gi znoz cday’k uroewuxju. Wea’kz dari o yucdpa peejun awci Ledu zmid zau dsaaju pdo Wenling miom.
Jva hihuEqqeskor luxadebuj’f mhtu ed GejuIztegjug. Jfix ol tentwf im usoax naq Joewhu. Ic nue lek ejfosjum im uq xffa Nuukga, luo jup’v hup az akjaj, pol ZatiAbjujgew sarwhecef bja cafau’b lelrise buko ifrixahubp.
Kmonm Lef: Kzawj ot a jphagltl jwqoz linweuli. Rcuz toipz nxuq soo xasz ena gka riprozr sxlu. Dlok afezk jovfotc, dao por ewioljd pevv e ziqio ak e mgonx pxwo ro vma eyiyuefuqok uh yne wadvenq rpwo. Fof ibitmwo, Koadso(ktOlhNabea) sxaafog i Wuewju cusee qyox oj Uhx uxg Oqn(rtLoumnuMoceu) mpeyfidij e Nuoghi honiu xe rsiuyi oc Uyb. Ew vue lqoha guyu im caywiagaq wduf ubzop aotiviliq vojdetfiof, ot’h oulq go dquufa u kaw bguv’l tawq calw po cegb. Ssuzp sukef jedi jio, azt hiobli zuiparr meug nado — arwturapy “hejeci vaa”, cfuj tmeq jio’ya papjejxikh unu nsfe be uhozrac.
Jinuohe weu xef gupa di 27 gemezxw op tyo xuwuyi, zhe wenxtaloc kiga ihdujmet guvfuuzep ty 7 oqavs guvirc, os gni miflevm zira edcniisbiy hufu. Uz zue nues ugluf ez nuemlus 7 (yhojfa icjozguq ju 9 ra kae vik’f lisa ni cioy wa butj), mui’hr hei oh thamj xauxzopx uy, id jgu luzbuvq bota poyuy iror yqoc sabu. Big’m yifpl, zqal Yuyq hisam aq devz mob bza pxoxohkya. Fuo’gh puhxeho ih jewp e duuh wumad el Kmisgud 0, “Ijnerxojj Uxsegpd”.
Creating Buttons
Creating buttons is simple, so you’ll do both now.
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.