Contents

Pointer interactions

Support pointer interactions in your custom controls and views.

Overview

iPadOS 13.4 introduces dynamic pointer effects and behaviors that enhance the experience of using an external input device, like a trackpad or mouse, with iPad. As people use an input device, iPadOS automatically adapts the pointer to the current context, providing rich visual feedback and just the right level of precision needed to enhance productivity and simplify common tasks.

UIKit automatically handles pointer interactions if you’re using UIButton, UIBarButtonItem, or UISegmentedControl. If you use custom views to display your content, you must define pointer effects and styles yourself.

For more information, see Human Interface Guidelines.

Specify custom pointer styles

To add a custom pointer style effect to a view:

  1. Create a UIPointerInteraction instance.

  2. Specify the pointer interaction’s delegate (an object that conforms to the UIPointerInteractionDelegate protocol).

  3. Add the interaction to the view’s interactions property.

  4. Add the pointerInteraction(_:styleFor:) delegate method.

  5. Return UIPointerStyle from that delegate method.

This example uses a custom helper method, which you typically call within a view controller’s viewDidLoad() method:

func customPointerInteraction(on view: UIView, pointerInteractionDelegate: UIPointerInteractionDelegate) {
    let pointerInteraction = UIPointerInteraction(delegate: pointerInteractionDelegate)
    view.addInteraction(pointerInteraction)
}

The pointerInteraction(_:styleFor:) delegate method is called when the pointer enters the view’s region. The following example shows an interaction that applies a UIPointerLiftEffect effect by returning a UIPointerStyle object:

func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
    var pointerStyle: UIPointerStyle? = nil

    if let interactionView = interaction.view {
        let targetedPreview = UITargetedPreview(view: interactionView)
        pointerStyle = UIPointerStyle(effect: UIPointerEffect.lift(targetedPreview))
    }
    return pointerStyle
}

Add interaction animations

Including animations can be helpful in pointer interactions, especially when views contain elements that interfere with pointer effects. For example, hiding the separator bars in a UISegmentedControl when the pointer enters the control allows the active segment effect to appear visually uncluttered.

The following example performs a simple animation to change the alpha value of the view when the pointer enters and exits the region:

func pointerInteraction(_ interaction: UIPointerInteraction, willEnter region: UIPointerRegion, animator: UIPointerInteractionAnimating) {
    if let interactionView = interaction.view {
        animator.addAnimations {
            interactionView.alpha = 0.5
        }
    }
}

func pointerInteraction(_ interaction: UIPointerInteraction, willExit region: UIPointerRegion, animator: UIPointerInteractionAnimating) {
    if let interactionView = interaction.view {
        animator.addAnimations {
            interactionView.alpha = 1.0
        }
    }
}

Distinguish pointing device input events

If you want to distinguish between pointing device touch events and touch events from other sources, like the user’s fingers or Apple Pencil, you can enable the UIApplicationSupportsIndirectInputEvents key in the Info.plist file. With this key enabled, your app can respond to specific gestures targeted at touches of UITouch.TouchType.indirectPointer.

For more information, see UIApplicationSupportsIndirectInputEvents.

Topics

Essentials

Interaction animations

Pointer styles

Pointer region

Lock state

Band selection

See Also

User interactions