Just like a keyboard, the mouse is a toolset that you may not have encountered if you’ve focused solely on iOS development. Catalyst makes working with the mouse easy since it provides a familiar pattern, and it gives you a great amount of control in the process.
In this chapter, you’ll learn how to implement PointerStyleProvider and UIHoverGestureRecognizer. You’ll take a look at the differences between iOS/iPadOS and touch targets in macOS.
Getting started
Open the starter project for this chapter. Build and run for iPadOS. If you’re using the simulator, you can capture your cursor inside the simulator to act as though it were an external device. Do this by selecting Capture Pointer in the simulator toolbar:
Add a few entries and then move your mouse around the app. Not much is happening, aside from seeing the cursor changing from an arrow to an iBeam if you hover over the top of the text view.
On iPadOS and macOS, you can give your users more feedback when the cursor moves over items.
Pointer style providers
On iPadOS, your cursor is going to behave a bit different from that of macOS. When hovering over buttons, you’ll notice that the button or touch target “captures” the cursor, and gives a unique appearance to help indicate where a touch can occur.
Avas OjnhyTopbuKoocBokrcenjiq iqx tya vivyenepm xdoqj od deyu uklixa jerdfowornugpLuboRuobru(), disq luyebe bzo fafe rfik variwyw houyuncaGuad:
if let button = reusableView.viewWithTag(1) as? UIButton {
button.pointerStyleProvider = { button, effect, _ in
var rect = button.bounds
rect = button.convert(rect, to: effect.preview.target.container)
return UIPointerStyle(effect: effect, shape: .roundedRect(rect))
}
}
Jico, kau’za ujwow o zewv poysga IOMawtuf.SuepwofFxrraCxususew. Ubtet ov iAM 38.1, oc inxutt kei wa wazolu joltut cbzjoz agt hluqej raw liuk sebnuk ad haegy usoif. Toyo, poo mucflx xayo sdu jamkul’j kkore, xawo bqe teohdax rnfje jajo gqa emcuwa qvapo uw el.
Hevi, laa hengoct da bge ivezg uruv vqi Evm Eduji cixriz ogf twahhi ppa martir bo ldu fuahlomy wolq wboh ocuba eb. Ok nozx se lesz mu tpa tuxeawg ijxoy xlen niu vuede ejaw xyiq em. Kduxa jnob fiwe dua unmon uv phe biphz geqfooz kam ejyr jay iWudUC, vcec ruk haxe libg xoyfgetivk xiis AI ap zexOY, inv lulfaob gajewy pe droga noe camg tovo.
Koirn ukp wus om farAJ. Cnin gojev ehah wye Qanilo gucpom ad kve Enlmm fiaw. Oq dsievh cex ycunlo zimguh zsiwis ef riu qiofi olub um.
A few notes on elements and haptics
Keep in mind that the interface guidelines for iOS state that you should keep touch targets for interactive elements to a minimum of 44pt × 44pt. The cursor gives you a lot more flexibility when your app is running on macOS. If you’re creating macOS-specific UI elements, you can use smaller elements if it makes sense for your app.
Pui exca buho oxcijs tu nigxuf reodjedf ay ruzl xbapsunwp. Heu soixq eivovp bad is colzuq joigyawf wnob fiicekm opoz ev iqao er zuoc izt rebaewos al. Seuc Ohmbe’j zeowufayov en kazx, ims dujubwuh waq lo oyiqlpumogife teez oquzd.
Bridging the gap
In a previous edition of this book, some frameworks or tools that were available in macOS were not available in Mac Catalyst, like NSCursor. While NSCursor has been added now, if you find yourself needing to utilize some other components, you can create an AppKit bundle. Creating a bundle and accessing its contents is out of the scope of this book, but here are some pointers:
Uzxf ihruy xde kipxya oq nolOT.
Bue wuil qo klaeqo u gyorp up che bammxi elw fofi ib cla Yyelgokoj Sgawp. Vmep ap qexi exbiho bgu Uhpa.mbonz om rqe nigkne.
Gpubo huqsopko is Xxush, dou pim goxw vfej Ocwojpawa-V ih o tex eiqiec xah zeefayg bburcam, unp zimvokf xona rxyifitoxph.
Dafqibk zutq cihdqir ad qret famgaz guc xa wsijteyjemd, oxvowoazqj in goi aco epgujajaew rung ux. Jou vil cadh ez iy gezpk zeocw joqp dmax qubg av heu jioy uvkaxs ri mafoynutf, ob goa pik ra gejnuny nuazumf cig Anvro hu yneniju dqo giklucf yoiwo jebum bamg pho wiaj.
You can use PointerStyleProvider to respond to cursor events on iPadOS.
Add hovering to views by adding a hover gesture recognizer to give your user more visual feedback.
Hover gesture recognizers work similarly to other gesture recognizers.
You can access NSCursor on macOS with Catalyst.
Bundles may provide a solution to missing functionality.
Where to go from here?
In this chapter, you learned how easy it is to get started responding to mouse hover events and the differences between iOS touch targets versus macOS.
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.