{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

TapGestureDetectorKt

public final class TapGestureDetectorKt


Summary

Public methods

static final @NonNull PointerInputChange
awaitFirstDown(
    @NonNull AwaitPointerEventScope receiver,
    boolean requireUnconsumed,
    @NonNull PointerEventPass pass
)

Reads events until the first down is received in the given pass.

static final @NonNull <Error class: unknown class>
detectTapGestures(
    @NonNull PointerInputScope receiver,
    Function1<@NonNull OffsetUnit> onDoubleTap,
    Function1<@NonNull OffsetUnit> onLongPress,
    @ExtensionFunctionType @NonNull SuspendFunction2<@NonNull PressGestureScope, @NonNull OffsetUnit> onPress,
    Function1<@NonNull OffsetUnit> onTap
)

Detects tap, double-tap, and long press gestures and calls onTap, onDoubleTap, and onLongPress, respectively, when detected.

static final PointerInputChange

Reads events in the given pass until all pointers are up or the gesture was canceled.

Public methods

awaitFirstDown

public static final @NonNull PointerInputChange awaitFirstDown(
    @NonNull AwaitPointerEventScope receiver,
    boolean requireUnconsumed,
    @NonNull PointerEventPass pass
)

Reads events until the first down is received in the given pass. If requireUnconsumed is true and the first down is already consumed in the pass, that gesture is ignored.

detectTapGestures

public static final @NonNull <Error class: unknown class> detectTapGestures(
    @NonNull PointerInputScope receiver,
    Function1<@NonNull OffsetUnit> onDoubleTap,
    Function1<@NonNull OffsetUnit> onLongPress,
    @ExtensionFunctionType @NonNull SuspendFunction2<@NonNull PressGestureScope, @NonNull OffsetUnit> onPress,
    Function1<@NonNull OffsetUnit> onTap
)

Detects tap, double-tap, and long press gestures and calls onTap, onDoubleTap, and onLongPress, respectively, when detected. onPress is called when the press is detected and the PressGestureScope.tryAwaitRelease and PressGestureScope.awaitRelease can be used to detect when pointers have released or the gesture was canceled. The first pointer down and final pointer up are consumed, and in the case of long press, all changes after the long press is detected are consumed.

Each function parameter receives an Offset representing the position relative to the containing element. The Offset can be outside the actual bounds of the element itself meaning the numbers can be negative or larger than the element bounds if the touch target is smaller than the ViewConfiguration.minimumTouchTargetSize.

When onDoubleTap is provided, the tap gesture is detected only after the ViewConfiguration.doubleTapMinTimeMillis has passed and onDoubleTap is called if the second tap is started before ViewConfiguration.doubleTapTimeoutMillis. If onDoubleTap is not provided, then onTap is called when the pointer up has been received.

After the initial onPress, if the pointer moves out of the input area, the position change is consumed, or another gesture consumes the down or up events, the gestures are considered canceled. That means onDoubleTap, onLongPress, and onTap will not be called after a gesture has been canceled.

If the first down event is consumed somewhere else, the entire gesture will be skipped, including onPress.

waitForUpOrCancellation

public static final PointerInputChange waitForUpOrCancellation(
    @NonNull AwaitPointerEventScope receiver,
    @NonNull PointerEventPass pass
)

Reads events in the given pass until all pointers are up or the gesture was canceled. The gesture is considered canceled when a pointer leaves the event region, a position change has been consumed or a pointer down change event was already consumed in the given pass. If the gesture was not canceled, the final up change is returned or null if the event was canceled.