-
Handle trackpad and mouse input
Provide a more versatile experience when you optimize your iPad or Mac Catalyst app for indirect input from trackpads and mice. Discover how to make your app responsive to new events from these devices. Learn how to work with pointer movement, enable pointer locking, handle scroll input and trackpad gestures, and accept or reject events on your gesture recognizers. We'll also show you how to implement advanced features like changing gesture behaviors with keyboard modifiers or pointing device buttons to delight pro users and bring a richer experience to your app.
To learn more about pointer-based interactions and to get the most out of this session, we recommend watching “Build for the iPadOS pointer,” “Bring keyboard and mouse gaming to iPad,” and “Support hardware keyboards in your app.”Recursos
Vídeos relacionados
WWDC22
WWDC20
- Bring keyboard and mouse gaming to iPad
- Build for the iPadOS pointer
- Support hardware keyboards in your app
WWDC19
-
Buscar neste vídeo...
-
-
1:49 - UIHoverGestureRecognizer
let controlsHover = UIHoverGestureRecognizer(target: self, action: #selector(handleHover)) @objc func handleHover(_ recognizer: UIHoverGestureRecognizer) { switch recognizer.state { case .began: // Pointer entered our view - show controls self.showsPlaybackControls = true case .ended: // Pointer exited our view - hide controls self.showsPlaybackControls = false default: break } } -
5:33 - prefersPointerLocked
class GameViewController: UIViewController { var shouldLockPointer: Bool = true override var prefersPointerLocked: Bool { return self.shouldLockPointer } func disablePointerLock() { self.shouldLockPointer = false self.setNeedsUpdateOfPrefersPointerLocked() } } -
5:53 - UIPointerLockState.isLocked
if let pointerLockState = self.window.windowScene?.pointerLockState { self.observer = notificationCenter.addObserver(forName: UIPointerLockState.didChangeNotification, object: pointerLockState, queue: OperationQueue.main) { (note) in guard let lockState = note.object as? UIPointerLockState else { return } gameEngine.performExpensiveOperationWhile(lockState.isLocked) } } -
9:54 - UIPanGestureRecognizer.allowedScrollTypesMask
// Enable scroll input for touch surface devices self.drawerPan.allowedScrollTypesMask = [.continuous] // Enable scroll input for scroll wheel devices as well self.pullToRefreshPan.allowedScrollTypesMask = [.all] -
14:48 - Requiring a 3rd mouse button click
self.thirdMouseButtonTap.buttonMaskRequired = .button(3) -
15:07 - Changing response for .alternate keyboard modifier
func handleHover(_ recognizer: UIHoverGestureRecognizer) { // Show chapter controls if alt is pressed let showChapterControls = recognizer.modifierFlags.contains(.alternate) // ... } -
16:38 - Only handle secondary clicks
class SecondaryClickGesture: UIGestureRecognizer { override func shouldReceive(_ event: UIEvent) -> Bool { // Must look at the event’s mask, not the gesture’s return event.buttonMask == .secondary } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { // Touch handling code ... } } -
17:36 - Only handle secondary clicks or control clicks
class SecondaryClickGesture: UIGestureRecognizer { override func shouldReceive(_ event: UIEvent) -> Bool { // Must look at the event’s properties, not the gesture’s let secondaryClick = event.buttonMask == .secondary let controlClick = event.buttonMask == .primary && event.modifierFlags == .control return secondaryClick || controlClick } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { // Touch handling code ... } } -
18:10 - Only receive hover events with the .alternate modifier pressed
let ccHover = UIHoverGestureRecognizer(target: self, action: #selector(handleClosedCaptionHover)) ccHover.delegate = self func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive event: UIEvent) -> Bool { if gestureRecognizer == self.closedCaptionHover { return event.modifierFlags.contains(.alternate) } return true }
-