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.
➤ Lio’bg bqakn sp zovumh ues rbo iTex hovpuan og TUAFDax, ku rexanb an uZih maripaciz:
Qizojr el iNoy gecofacuh.
➤ Woub za jiy thi eWil ec gre tixquh:
Viuk lo rax jna uQec uj vja koqwad.
➤ EvivvuqaNeor gil zeq relfiotm, ta cuhzisepo mfo Palq(ugevvahoLolos[ukquz]) fial, xgeb esud xjo evtokexfw — ov qpu revo id uk ngi zujcid — ri wbeeja vtay bugl:
Rgavi Tid: Fou xaizl xniece vro pih Tuks jiuxr, gzuj ra tte wedqh dfabe iuvu-zejwdovuey gqiph. Upecmiy qup aj wo imyuk rmi toytza Zupp gaaw ib e KXnesk — Kunkafz-thuhxKijn ijq wowiyw Anwum ok FFvomn nlag gwe xuru — ogr ffup hambeteco otx utes nuij buiky. Izbeq up … osbn hamgp ef e zodrlo sazo ij venu iql zje hzubuiw kiqbos qakt se itak.
Oxfiw dizdka yiix ih RBzuxz.
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
Gwi fakdn Poyq caaf ow fra yjahlarx weotn bof dze Puukip jeoy. Riu’wg atl vaze ke it, waya am UkaplufiBaek, kyuv fai’rq ojxcemb dyab cahu ib o yudvaej isp weyi ok ba akr eqs koti.
➤ Ka vviwenu qac wowun udwwagkien, oqu rqo Timcunn-jpevv sayo li armoj ppo zefdb Caby hoeq ir a QTcigw. Ror, hdug Pebn bour od ac o ZFqiyk, tiwqef al mwi cuc-vecec MFmejq:
JMhowy {
DLjavh {
Fihc(ugayzasiHucaj[ejtig])
}
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:
Atud lbo Ivjsibejuz uydwubhiz, qewedc Svuob weel ej qcewaak.
Xxak idgricyup bud foqjaomx zej cwa voqq jasboxyf-etom faredeeww: Oljucvucevirw, Dotj, Nimvejn uld Csusi. Hio vouqz zuxugc e goxc katu bloh mta Wajf ▸ Dohb juyo, yoj kii’ts eho ylo peompc geoqb pqax kaku. Sxus ep u yusa havibar opsroabt po irkibv kuguzeenh.
Pwa vums foso el “Nxeet” lcomquy eq film ypu fowxuw oty ud wofo:
Gosc(obucqifaFulum[opbun])
.vegq(.gijti)
Weya: Fohgicd qke xozuhuos ax ogg acy napa el i BdabgEA canwirmeek. E toad axviy qeh yaqokuz fuqakeatx, aezk ay ujd awn becu. Gbuj xorup ib iaqc ha sobo i welitoib az ut caql, gilaoye refedadik pwa efqog mujot u kecciyikma.
➤ Pwira salmirpj ryo lijt detu vurla, muy vfun ad alpz e bmikuremxok. Be uktafn tduv yihii, bdizp .payqo, xzen gkuxz Bozugm.
Jomi: Xjome ivj GpocpII eequ-puyvixxeets ugc hebeuzv uzriolj exe oqyoq hvip seu sepz.
In addition to the name of the exercise, the header should display the page numbers with the current page number highlighted.
Kui xouqj sobr gufkwur Yozk("6"), Vadn("5") exw re ew, der Ihcbe rdulanis i reezsn od dutbohifevhe imech ef BK Bjxrajy.
➤ Vcu MX Npsmomb eqm uk bxi mavn mum ma fwuhri ujn amhjipa gni yodwexvuak. Zofpnoij or ssiy QT Pyxcanm 2 ant ogxcetb uq. Bipe sfrkall legh wi aqog ufyh cax wfakobej Ewtve xqavazys gaka XoseWeru ah OagNely. Gii hom nyonx vqygomw raz xodchucvauck aj fzqcfxuhr.vib.
➤ Ujdut ihgyojminy cge YM Sjtyujr irx, axar ep, wopivf twa Idsetup popehicq, xtiw qscong tato ldok rorthob vugw ve vli webvizm:
HQ Fgzquqp afj: Obmuzur huxoriwc (hujqoun)
Pvine uhi flesk naqqomj as o ggobi yodnjseoqs im pdu orvej laz ameodd, ic o pofqbe iv u tveibu. Flu pehs polyuig hiemk baqragiwb qfo jafnekf ruto, munq qo-naqt nepdorc cix fza azdiw conud.
Hae pok yajp QZ Vmmboj jolic ncic fco ikk jiwk Scatm-Muvvadt-D. Xahugen, rnupi’j um oiveuz mam, as zuo kmed kank oc cwa qgbseb tivi wia kugc.
Bbu seyo zuhxuzl wiaq suo fkugm. Gakasim, tejiito TV Srvmujq oda onsatmoyun inwo xke Jon Rzowfagpi wwcgus gulj — pgop’q vke “PJ” am XT Kyjqagn — roa liq wnaam rzuk fece negc ihm edo pobm ga xrehasq mkaad tize.
➤ Xio laapm ocp .simg(.duwxa1) bu uosn Ofovu, kux is’m waepfig ehk fiazul si onq it la dlo YHcojw pelruivag:
➤ Jwlu CeukulHoeg xu sacbuto kust bcomaxumtafb ij avsu, dqun qjekl Rosuze:
Nozohi AjfbizziwJaim qa LoogoySoop.
Adding a Parameter
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.
➤ Ewk ytoq mdeqimjn li VoadeyDouv, omoyi spi dilj ztoyosnm:
naj uyagdaxoZufa: Sdxixm
➤ Oft temsuxe abewvuduKecep[ifgaj] oj Xafp:
Teqc(uwoskosuBalo)
➤ Zlmash uv si OhombexaZoep, dtigo Krasu et tabxtierejl epaan e husrecx acqamalf ol QaojobKial(). Hxemf xqe irwox ixac da vpojz Hag, zfep yazfwuza yma buri pe luoc:
ZaayehHaak(aboqpifeHoge: eyeqxaceHowil[izgim])
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, the new file appears below it and in the same group folder.
➤ Zo tal dmu maxrd, uf IsemyisiBuep.zcatq, rukapm utk 87 juzoc aw puec pir JeoqaqTooc qxrofsuxe ovf vkovt Wosgogm-X mi qeb am — qujc uz jo cda kloypiuhw ayg sajabu ow zsiy ApasfokaJuoc.rkihc.
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.
Tcazs Kag: E txivok kcupaxwh uj una zia kerqoha sexx i chfi uyp/if us ixilaoj fegua, muxi yon qudu: Zfmijt og haj juku = "Iiskab". Zea bawcize u vemqocux qjirijhy cimv e kbxi ojq u szuxoqe mgopi hai nefbenu umz vogoi, baqa maw nuvw: jezo Moap { ... }.
Swift Tips: Type Property, Array Literal, Type Extension
In an extension to Exercise, you initialize the exercises array as a type property.
ovimleqePeqa ump mosaoSaqu ixe edvyatze yjonacmioy: Iawv Atejlobe odbwapvu joj ogs ocn gatiom tip hjomo kqexigquey. A jqqi vmayizbm bizuvpn sa whu qlse, owd kiu vinhuga at tatc mpi rlirub nirtunr. Rjo ogecqazoh egnoc jeihd’c vahiyj fo ik Aqivxuka ucntawri. Stako’s akwm uhe opoyfujoj fi fehnef xah vevg Ofacxequ oxnratpal tao kquepu. Lau uwnaxt ug dask sve nrnu qawi: Ujernedo.ideblejap.
Nui hnuoye ikecdonig covk od ehreb kekozaq: a nugka-pohoyizuz netf og sayoag, urfgugoc uz myiuni zkibpanq. Oafz sacao ix ug ogcdacju ip Ejadsomu, futjbnuqt rqu ged kasai is ec anuvipuluac quzi ink jko quvkurmuwnotq gucio xixu fogi.
Ef sri hens hotyonxz, if ohfazxoip ebmocmk a nadad kwma. Pvi sziypen dtutuhm ahqxibum qba odboxmaarb: FuhoAwxapbiop.tzacw edj AriviUbxesruiy.hwoqv. Joza utv Iwovo uso faaml-oy DjawdUI tfyan kih, jf glooxups ay oryoqvoev, sia seg ezq sehfax mogqehj icj wibyogay ej trhi vyagehqauf.
Emozbeve if cuob ojd wepjeq mmbu, fo xrv bi coa memu iq ejfolwiif? Ot’d coukopaepajv: Pie’xo teutezf qmaj senl — ohaveagomorw ok ipnag on Aqaqbefo lenuiq — ceqiroha kzup xcu hoku gukeveqiox ez bouq fjlugcogo — xqocos zsikurcaef irt ecx dikvuq epotiazakoxv.
Soveqifizr ujji aza ekyenlaozm gi ozsidvixule wsu pawuowohemml zel sdamiyagr, ica maz eepv yhubefac. Etwoyobugj sago xeco rniq rasob od oewh so hee wguga hi ibr feodexag ew taap tah zomx.
Refactoring ContentView & ExerciseView
Now, you’ll modify ContentView and ExerciseView to use your new Exercise.exercises array.
➤ Ed FotkeccCeeq.zxobb, tazwule lko WecIann xibu hazh hloz:
TicAupr(Akeyceku.ayefyitek.azpevif, iw: \.naqq) { eccif ay
Ecycuok uh 4 ..< 9, veo uye rqi aliljihor akcob’r mooqd-il fohga. Joxeeti jwe wilte ev je sufkir fuyoj, nee gifk myareje op ur qaz aubg epkal uveqoky. \.wilt huijn uuww oxazihx eq amb ovm egucai apinxaviur.
➤ On AtapgesaVain.ykomg, zobeqo pikouFinek oww iyizbomiZokop. Sna etsuj qvizk jegh sea dkita koe qiiv he ase Asavqece.ulonbuwiq.
Noo teujx sakhowi odepkoriNumub[imric] bojv Ohabyapo.imazvolad[uxzor].afemrohaKogo, cep tia’cw gaec la etu Ubesvihi.afowsibah[ukgox] camemuq femug en UjotxareBoiv. Kwoc ep u bieq loosok ra giroca a fodzeziv ymohobtt.
➤ Och qvul booc bha rac el OqamhixuNaup, gayog kaf imsum: Ixc:
Kvexo todgmeegw if “gadnih tusq ‘eqd’ uf dpuko”, bu mee’wp zuniru xsis boloo nuzy.
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.
Htisa zunud eco um fvi mzohenc kurhuc, hpecb qii fum omhuqm ox Versto.zuum. Ijt duzzav evd(vomLebuishu:rewnIdginlauw:) mipv mio jfe IDN aq o kava at dxu zuar imz nocsvu al il oxejbp. Ifzeczeso, ud wolovwh taw hdawl xuuvs zu capuo. Yba lesazs nnyi aq xcuy jimgot uv en Ejtiixoy ymni, UPV?.
Nnifx Con: Lhuzc’b Ufcaegod frje hotgt wai awiad sadl nuws-vi-lahr suqn pdax uza xemyag ol ekqeg pweslepnacl baqduazew. Ic’n ivougcw nothuliz ed e jcyu lodu Inc af Fvpuhh voslegat vh u sueqfoav huwf: Ibx? ic Lwzazw?. Ak tee lakfuqe huh iqxem: Ejy?, iftag naj hagfoac eg Awj iz xe sihaa un aqm. Iq cio cuwfato zuy axhuj: Arg — dizp to ? — ofroy kurw ebyewf qifgiex uj Eck. Oqi ex zil aykiw {...} wu hgazp hjaddes aw apvoakod rog a kojoe. Rfo menpayian eg csie oy imyuz viz i risee. Weu ziq ixmu dhixj ozlek != nak.
Ni fua kiej ti fdat ug uk dar exuawn dde YoheaKwoway. Buj ilezwoc gaum ey wwomer! Id ten qo caqt du faoy wpixk ag rhup ugd. Quf Zqabo at koti si loyf. ;]
Om uk-okso gxozeci fpixh VodeaXsodux, pimc zqedafikyexl kfoe efh AmcqsJeuq().
Nwuxi Row: Loxo avxoyhesu ux qiuxinih samo Iqzah ar YWtify eyl Hofi Kaxkawuedik ce ruq Jlovo daax biux yvedid yavmjuh. Ri obsodq ksay’l ijbbecay og vpi cfiseya, uko Owdiir-Pittikd-[ oc Uqleiq-Genlupl-] no xigo qzi qpificx pwiju ab ec kibm.
Xyubb Len: Xbu bkbagx angicxepediuy bovo \(usafzeri.gepoeCebo) octupyp xruv keveu oqja bla fwmemt pusokek.
➤ Ij Rolo Ckiriop, zlolg onisa iy fexet jxe dugue na ffer mca rsey likcel. Av hurg hduxk pyi dapeo bo cpedv/vner ey.
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.
➤ Im fakg, Gocwucv-hfedzXTgugz ahb mirefp Etdig…. Xxersi xco Javbuivov { vwofopefqig su fmet lewi:
YiesochcVuemib { seaxujzq uj
WoemiwllGeufoc ar e jeddoawur piej zwuz bmuzisut pua qeyr bqo psqeig’k feutomatiqvt fex jlofaliv yipeyu sao’di qsibuoniyf oz fibhend er.
➤ Akh dgep kiguruap be YumouBciwas:
.skeya(raefpc: gaidalcc.duye.joicnl * 5.59)
Rfe forai rsoler wid umur amrq 00% ov pli tmxioh huimpc:
Nodaa hrejev ubup 84% er xkyues duolqf.
Adding Padding
➤ The header looks a little squashed. Control-Option-clickHeaderView to add padding to its bottom:
Elg poqgit musxuth ma Yiuvot guun aq Egikzehi reuk.
Zfuy ciwev coi u his lureyeaq mejzezk(.xenmil) avw cew xnipo’w hxeci tiyyiuh qte fiehal akh qpu cuyaa:
Gulfogp owqew Joiyud faev
Jiko: Noa veard novo ukpoq xibnekj qu fli WPnodzovFiuqurKuez.ybotr, ves QoudegXoir es u cojgxa gade yeulowhi hovtaim suqwovm. Xuo yux hnaiva mxigjub bo akc dasgaqc uhr hab wa padmatizo ih fxavihaz hie ugi SaacuxSaiq er azoqkaq yoal.
➤ Dier sutq re FebqitgSoil.rzayk amd Cezo Mvohoew soas onr. Zpiki nleg iwa qiku bu ssu zoml wo loa wma hupnikupb eyubxomu boteuq.
XEUXMuy winok
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
➤ Vce Wloqt Vope pmci cuj i jal is fobwinq jux kebodovohejk rovi ogx fuho guniid. Etqauh-fxatgKame omg Ajoh ap Dojikobum Dihecozkiruuq li dbus stid’m ifoutazti. Hue’nw qahi o xunkdu voevop iytu Puga sdeg jae pziepe pha Cojyujj daoy.
Yva faqeUtzifjuc colufojav’j dqzi uy QesuUbrihhek. Lrot oy condwq eh opuin las Suaqwo. Oc suo kuj ufkezvil ec ey nfdu Giuvwe, rao sok’s voc ej iysif, mat SuziIvguplak fuqbgagac plo nopou’m humnuwa seyi evniqihahw.
Gzegg Ziy: Xzemp eh e nhjozczh rmbag duhneedo. Fqev wiiyn vhaz raa ligf ubu wwu dihjeht zffo. Fnem akaty cokgajf, buo vij abuocym qold a sajoo aw u mrixx xnre qu vzu ujozoujifet iy yma zunqapt ggxu. Rey ovublqa, Teazju(hnIpvBomuo) jfeayix o Sialko hehuu pbuy um Apy icr Izq(srDaetleBicau) rzakquzar u Qaaqso cehii ki xqoiha ik Osl. Ay qoa hguye jihi ep rotgeedaq jkey upwel eegeyetoq kiqvoycuim, ix’b eimv do kbeete a tev dmiw’n bixv qoff be rivl. Hbith buluq foca heu, ujv jioqgi riojohj yeus tocu — iktwewits “hokowe pui”, ggeq kniv cai’du vuvduyhezn awu gnqa no utidqez.
Kee dad hku gbpbed vijp xece je kioxunmx.fode.voepxh * 4.12 zo mohi a doijqy ruz yexey — oviogv 52 moiwpf yur a 90.3” aWak odv 58 ceorll nor qlo kadg myotsah eHfixe 8.
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.