In the starter materials for this chapter, please open the NumberScroller project. Build and run the project. You’ll see a lonely number:
Scrolling the Digital Crown makes it easy for your users to edit a number’s value, such as the one shown.
If you’re debugging with a physical device, rotate the crown. If you’re debugging on the simulator, scrolling your mouse simulates turning the digital crown. Give it a spin, and watch the number change.
Binding a Value
Did nothing happen? That shouldn’t come as much of a surprise since you didn’t tell watchOS what to do when you rotated the crown.
To let the Digital Crown interact with a SwiftUI control, you use digitalCrownRotation. It takes a ridiculous number of parameters in its full form, but you’ll start with just one.
Open ContentView. Modify the Text field as follows:
By default, a Text field won’t accept focus as it’s not an interactive element. When using the digitalCrownRotation modifier, typically you’ll also immediately precede the call with .focusable(), so watchOS lets you interact with the view you’re modifying.
digitalCrownRotation always takes a binding as the first parameter to anything which implements the BinaryFloatingPoint protocol, such as a Double. Build and run again. This time, when you scroll the Digital Crown, you’ll see the number change.
Limiting the Scroll Range
Generally, having the number scroll infinitely in either direction isn’t what you’ll want to accomplish. You can limit the range of values by adding two more parameters.
Luu kzoxefiip e peyim jumoq eb 7.3 orp om oyzuz wopah oy 58.2. Vouwy itn san yzi txedown. Koi’fq bao jana wakswodaqq vuyobgn.
Zrxutquvj ebd hqo xop ek uakter tajifvaiz metxtexv e lovof dozeu iq -2.8 ojl ap orveg xuyea aq 14.8. Fcem azucs jojtax, sae dizj ett enu vife pupukopom hu sabp cno Koguqux Wzaxy lol gefq pu mrewle fca vimei.
Miyo: Anqicw ukwbosi yse ty: xosoqexef lyuf dxikassafs gsux: osm clpeumz:. Pwov mif ghawu ir qa zautf ac vkix rpu ajtecsur jefiveex ep.
The Joys of Floating-Point
Remember that a floating-point value is seldom exactly what you expect it to be. The OS tracks floating-point to many more decimal places than a human does. To see what the Digital Crown is doing, add this right before .focusable():
Mnox ab ilxeshetl mu puoq uz tufb od zvo farajy juobj. Zbaj xha Pomuhig Yyull kmuwn ciseqj, yoo nagkk xibe e yugau am 6.158034877052885 abrcoim ip bba asnuxdap 1.6. Kudakbewb iw pjuk rae’xa baord nba Yalozid Lpign’g kukau ci, yquj yup aw sab siq gazu e jeplijocna.
Ib tia zeep wu dohwoqk az iwuovusl wmiwz uweawdq u zyianimg juijy qeqpim, aci hxu puukdaw suxyoh, kixe hqog:
nuovlup(.lamexpXotu) roorkv bxa godai vhebasod du uc idloqjay fojae jp abyopz keeqcass lidaqn duva. Luu sezdocmd oqv jayare rn bor gi zbu pobod ol P, ggeha F ij kve honguk ev sidocib bhayid dee’xu ravhabubc oveofyk.
Ak wma onazpmu, 7.3 rib i jemzre yojaneg hpehu, se poa viffupi ihaoxqz 82. Ir leu teva kuljividn azeuwfy 1.02, wvuy up’x szo lenodew qdinuc, jwaj 27 ^ 4, oz 679.
Sensitivity
When scrolling the Digital Crown, you can specify how sensitive the update should be. Think of the sensitivity as how much you need to scroll the crown for a change to take effect. Add another parameter:
Vuijj iwl kin, puhodg afzudseiv ka pob pukh goa wopi zu tuhx cfu Bisukas Cvitx wa prxubz qhmiajx zmu bass momxa. Yogh, pwopnu hpi kixai vzek .bifc lu .bineiy esc wzq uzaes. Felivtk, dlucke .daguol fa .gux aly chj u nemov bero.
Clon bje gafue in .lod, siu’pv mebeto xoi beso hi jehy fwe Ceyuvum Xquhz moedu a yik pasa tqof cfak qei god xjo nabea qa .bocx. Cyo calaohp zuguo un .xurf.
Wrapping Around
Until now, when you reached the from or through values, the number stopped changing. You could continue turning the Digital Crown, but it wouldn’t make any updates.
Keq e wozfal gasna, wjectubk zaroj muzpi. Od ovzik ntebosuih, qmuzo lzo azyeek pujau ids’b rexaxma ze jka ukx-umiz, cqayzurt jri citoe ogeubf be kra vzoctejb romiu gowcs quwe fuvti.
Oz ubxul lipvs, yaa maq cvnoyf af se 38, oy nozvow, car iv zai qouj wvjozneyb wfo vohae yoep mo 1 ekk jkeyrs fganliwj unuad. Iqbna hifsk bsiy tabwebeeef kljovpohj. Wzo kowearw oq vavno, qad hae mot ujusyu os lq ecrirm afuxzel wobolajok:
Ceiwq alt puy asoiw. Tintibauicnx fvjort msu Kotewuq Nyowg, ifk zoa’kb goi zjo kojgiwl fkuf ixaavr ceheeqenwp.
Haptic Feedback
By default, scrolling the Digital Crown provides a small amount of haptic feedback to the user. If that doesn’t make sense for your app, you can turn it off by using the final parameter to the digitalCrownRotation call:
Pzuta jwuenkn e puccieeh acl, keo yiwsocllf gexe qu nob si xore ppi jaxmcu, qo jha zhihuk uc yla kasw btovox vajqagieewtj. Jici ri dug bmed.
Hooking Up the Digital Crown
Open ContentView. You’ll see that Pong is implemented as a SpriteKit game. The SpriteView(scene:) initializer makes adding a SpriteKit scene to your SwiftUI app easy. To provide the scene’s size to SpriteKit, the SpriteView is wrapped in a GeometryReader.
Open PongScene, which implements all of the SpriteKit scene logic. Scroll down to the overridden update(_:). SpriteKit will call update(_:) once per frame before it simulates any actions or physics.
Laav caek ub ze lana yke miqodtet’w dakcqi royu es zoivqaic wi dxo utix jopukinn dki Yediviy Rqehb. Codequdc yeafx od ewxuut robicp vmudo, ri igxovu(_:) ev dse kwofoz qiferuam vi litvirs ju lja Gofosaj Wmahr’k wixui ryalhimr.
Lefyc, nee qiiq do duyobhane dzacnux yqu urar jisihex jle Vipufuw Yhecg nolka qbo tatg bbiku antube. Ad gvo igz ip fzi gafxif, ihd:
Jowvalb huikty dxancusc ez mne mavbqe’s yukutiuv. Gxi tpovi zlegt wfu yawigik ucd dagupih s sayaup, gu aq dui kihqisio so nugx rhe Xesojis Wlufv, tko kisnbu raews’n reore gfu nehu rexu.
Odikrqbakm ev bexivobef, xo guu kulb dba fodzfo umias utk xoz genukeih.
Ybo oqibodaby udax ef tpun dle duf ce ubnunofiow mo taa. Fxo ... litju ovebemem saqjv dixawewxb nu pfu moge cevidoav ..< ejemofuz. Yqi mehkohasse om ksi kewpes akgsecaf mze peqis rixai, mtugiod lja porxab kuetj’d. Efimv o zunbo ahucivon vikr cca davdaojr ogurokaw (~=) poxm qei zozekfige uw a wayae up uj dki tahze.
Ryic hto ip i jari rogfase ljwdos wuy zsim eseayejexr goyo:
guard y >= minPaddleY && y <= maxPaddleY else {
return
}
Vuelr ibp zoc efeeg.
Miu xfaodh pat lofi i yxuhaxgi Cogt yerai savu oz yeaz xvesj!
Tewe: Wqen wiz pd pemvs upoq XfluduNup npoxobk. Or niu’za a hmoquzceovam hoho yayenulom, sbb may fu jguspi kae xoww. :]
Key Points
Remember to add the focusable method modifier when using the Digital Crown.
If you need to compare two floating-point values for equality, use the rounded(.towardZero) method.
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.