| id | callbacks-events |
|---|---|
| title | Gesture callbacks & events |
| sidebar_label | Gesture callbacks & events |
| sidebar_position | 7 |
import {GestureEventFlowChart, TouchEventFlowChart} from '@site/src/examples/CallbacksFlowCharts' import CollapsibleCode from '@site/src/components/CollapsibleCode';
At any given time, each handler instance has an assigned state that can change when new touch events occur or can be forced to change by the touch system in certain circumstances. You can hook into state transitions using specific gesture callbacks.
When Reanimated is installed, all callbacks are automatically workletized. For more details, refer to the Integration with Reanimated section.
Note that some of these callbacks are complementary:
- if
onBeginwas called, it is guaranteed thatonFinalizewill be called later. - if
onActivatewas called, it is guaranteed thatonDeactivatewill be called later.
onBegin: (event: GestureEvent<HandlerData>) => voidCalled when a handler begins to recognize gestures. If onBegin was called, it is guaranteed that onFinalize will be called later.
onActivate: (event: GestureEvent<HandlerData>) => voidCalled when activation criteria for handler are met. If onActivate was called, it is guaranteed that onDeactivate will be called later.
onUpdate: (event: GestureEvent<HandlerData>) => voidCalled every time a gesture is updated after it has started.
onDeactivate: (event: GestureEvent<HandlerData>, didSucceed: boolean) => voidCalled after when handler stops recognizing gestures, but only if handler activated. It is called before onFinalize. If the handler was interrupted, the didSucceed argument is set to false. Otherwise it is set to true.
onFinalize: (event: GestureEvent<HandlerData>, didSucceed: boolean) => voidCalled when handler stops recognizing gestures. If handler managed to activate, the didSucceed argument is set to true and onFinalize will be called right after onDeactivate. Otherwise it is set to false.
onTouchesDown: (event: GestureTouchEvent) => voidCalled when new pointers are placed on the screen. It may carry information about more than one pointer because the events are batched.
onTouchesMove: (event: GestureTouchEvent) => voidCalled when pointers are moved on the screen. It may carry information about more than one pointer because the events are batched.
onTouchesUp: (event: GestureTouchEvent) => voidCalled when pointers are lifted from the screen. It may carry information about more than one pointer because the events are batched.
onTouchesCancelled: (event: GestureTouchEvent) => voidCalled when there will be no more information about this pointer. It may be called because the gesture has ended or was interrupted. It may carry information about more than one pointer because the events are batched.
<CollapsibleCode label="Show composed types definitions" expandedLabel="Hide composed types definitions" lineBounds={[0, 4]} src={` export type GestureEvent = { handlerTag: number; numberOfPointers: number; pointerType: PointerType; } & HandlerData;
export enum PointerType { TOUCH, STYLUS, MOUSE, KEY, OTHER, } `}/>
GestureEvent contains properties common to all gestures (handlerTag, numberOfPointers, pointerType) along with gesture-specific data defined in each gesture's documentation.
<CollapsibleCode label="Show composed types definitions" expandedLabel="Hide composed types definitions" lineBounds={[0, 8]} src={` export type GestureTouchEvent = { handlerTag: number; numberOfTouches: number; state: State; eventType: TouchEventType; allTouches: TouchData[]; changedTouches: TouchData[]; pointerType: PointerType; };
export const State = { UNDETERMINED: 0, FAILED: 1, BEGAN: 2, CANCELLED: 3, ACTIVE: 4, END: 5, } as const;
export const TouchEventType = { UNDETERMINED: 0, TOUCHES_DOWN: 1, TOUCHES_MOVE: 2, TOUCHES_UP: 3, TOUCHES_CANCEL: 4, } as const;
export const TouchEventType = { UNDETERMINED: 0, TOUCHES_DOWN: 1, TOUCHES_MOVE: 2, TOUCHES_UP: 3, TOUCHES_CANCEL: 4, } as const;
export enum PointerType { TOUCH, STYLUS, MOUSE, KEY, OTHER, } `}/>
TouchEvent carries information about raw touch events, like touching the screen or moving the finger.