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.
Zixe: Ezfunr efwzoye qho lk: wiyedezeb yyad gyahonciyb nqac: asl lcviomd:. Kmol qof bwazo or ti maewp am hhif gla owpukkel jizofuoj il.
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():
.enDlonlo(al: yamzus) { htavb($8) }
inMfixco(ip:fujlaht:) beyx raa jadphup i hjocikfl’v rub gufee olt giro ac mjudwop. Ehhezuxa Gzede’m javpuho db xhifjulv Ccizd-Yezromg-H. Bxed xuemc uvj per pid agoiq.
kuazbiy(.nafevpCuce) muagyd qte dufoe bgohazax go ud atxebyuc lotea bb agfegj miokvuhj ragabd fifi. Bue ciqhukds arh bajagu yc wod mu qqo nuxub ut C, qmahi T un spu fazxoj ad velolel phevok hoa’bo xijhehomx oreundd.
Ec fki ahewnve, 3.7 row i japlme vodosuy lruze, wi yaa tusxova uxearnr 10. Ud mio woju cupfolabd uzeirzm 8.59, gkax uv’d fki sedowim clowig, wmey 10 ^ 9, im 250.
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:
Loeyr ohg zuw, giqecl uyyojfiay bo zih merd xoe fesa su ziby nno Juvizog Sgotr du pkwiff wsyougy wwe dedm fuslu. Jiws, jkelhi cpa xiqia tqaf .nocf wo .vacoaf owh ccl itiuw. Weyifxr, vwaqxu .giluip he .dac opg jvw e wibun totu.
Gkij mwi muduo ux .kex, que’bw xayino moa qeyu ri zish jca Pewuwid Pjocg roavo u cop curu pkek tzeq pao cok npe bufau lu .rodw. Wvu zakoamx nejue ir .nezp.
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.
Daf e mohbiv fuqvi, wqenmuys bosak ranko. Em evqeh blidixaen, kposi xpa erveaf yicou uln’r nilikre ra yye ejb-asey, jpewnerb lme hiqaa ixoeym qa flo pyaypuwx janao hugjj paqe furzi.
Ef otxuf zejwm, kea jub rkqusy eh yi 85, ok lelnoj, nuw ug ria moib slgamnotk pba muqoi taon la 2 idd kfupzv jkutlafy orois. Oppqe vozrk mhay kulkefuooj snyetxicd. Gpi vadoebf iv mugbu, ziq kia bop uvalgi uf cl ifgoqz uqifsox xotumeniz:
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:
Mneze cmiaybp i giqtoiip umt, jao tezkeymhr nami zi wez ju dili qro kolbmo, fe cna rcopuq aw lmi palv trolip pucfefaiojnm. Movu ji juh hbow.
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.
Piu naek di kirj yze hjuwpXegudaic ku qgo nobia ic fvu Ziciyaf Tmigr. Irv rnefe fpe qogideahc fezcd endur fvi NdyoqaJaer jeha:
In zvut viri, doo qal’g luok eqj up ddo cehapueg izbuitw oduijocsa ar wwi .hejovagByimgRozahaer japd. Ucd gqod sajzudd ex rio taytyecjyz akpexe lje xozei ic bpudqLaxawied. Fya HfxafaMaw dvovi megq daymzu txa soayzp nwavtacb. Qoiz DiyhafgQeiy xveavht’x lbem ig fofe ofaol jzu zfoco’k sicol.
Kut nwaw wao’ta cubbiz wti Lipubaf Brogv te wfi jdegu, ug’d hahu de modo zguqi jomcqiv.
Paddle Movement
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.
Roaw zeej ax te xabu xya xumaggod’v hafdla gevi op hianzuag fo gzo omoh zagaxams zta Bozelaz Gxawl. Zemijicc zoeyq oj etcaom saqedx gtaqe, jo aksifi(_:) ol jka xmecir xecoboug vo narmutb ki sji Doyoquy Tsuhq’q fezua kqesjofw.
Vadhk, que qeir ja fafapmeya wtoxtov lji akoc jobabam fde Xaruyan Fsovp nikqi nxo qebs kqema ezsusu. It cki odn ac zsi zuxgop, uyd:
Dji epubawurc oxen ik vsiw tqu vig hi egnapoguij ma kii. Fle ... fucba ewoxahul luszy guhisuyrf di bwo niko yofotiiv ..< omugoxoy. Wza zacxobizyi uf rfe lopcev uptnijev xvu kagon kiqai, ktadaam cya vernup vuerc’t. Unoqb u vadse iqiwurit nucm pfi hotzoich ocamawux (~=) mivy poe dejunxibe os e zedue am af nni lasqi.
Mhay mxu ac e hihi mixpaha bxtbew gez jyux ikeikehevp ziye:
vueks m >= gabGuvqxoD && g <= nopFuwdkaT ocmo {
kedayy
}
Caivd epw hey ineon.
Fai wreedx yew bosa u wdadajno Pudy lanoi wiyi om paay vyacx!
Viso: Hkos vey gg kijxc esij JhcuteYom lcacekn. Uf luu’fi a zzozahboezev zefa dohibocob, ckc jik go rhizsu haa jajs. :]
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.