react-native-gesture-handler#GestureHandlerGestureEvent TypeScript Examples

The following examples show how to use react-native-gesture-handler#GestureHandlerGestureEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useAnimatedGestureHandler.tsx    From react-native-gallery-toolkit with MIT License 5 votes vote down vote up
export function useAnimatedGestureHandler<
  T extends GestureHandlerGestureEvent,
  TContext extends Context,
>(
  handlers: GestureHandlers<T['nativeEvent'], TContext>,
): OnGestureEvent<T> {
  const handler = useCallback(
    createAnimatedGestureHandler<T, TContext>(handlers),
    [],
  );

  // @ts-ignore
  return useEvent(
    handler,
    // @ts-ignore
    ['onGestureHandlerStateChange', 'onGestureHandlerEvent'],
    false,
  );
}
Example #2
Source File: useAnimatedGestureHandler.tsx    From react-native-gallery-toolkit with MIT License 4 votes vote down vote up
export function createAnimatedGestureHandler<
  T extends GestureHandlerGestureEvent,
  TContext extends Context,
>(handlers: GestureHandlers<T['nativeEvent'], TContext>) {
  const context = useRemoteContext<any>({
    __initialized: false,
  });
  const isAndroid = Platform.OS === 'android';

  const handler = useWorkletCallback((event: T['nativeEvent']) => {
    'worklet';

    if (handlers.onInit && !context.__initialized) {
      context.__initialized = true;
      handlers.onInit(event, context);
    }

    if (handlers.onGesture) {
      handlers.onGesture(event, context);
    }

    const stateDiff = diff(context, 'pinchState', event.state);

    const pinchBeganAndroid =
      stateDiff === State.ACTIVE - State.BEGAN
        ? event.state === State.ACTIVE
        : false;

    const isBegan = isAndroid
      ? pinchBeganAndroid
      : event.state === State.BEGAN;

    if (isBegan) {
      if (handlers.shouldHandleEvent) {
        context._shouldSkip = !handlers.shouldHandleEvent(
          event,
          context,
        );
      } else {
        context._shouldSkip = false;
      }
    } else if (typeof context._shouldSkip === 'undefined') {
      return;
    }

    if (!context._shouldSkip && !context._shouldCancel) {
      if (handlers.onEvent) {
        handlers.onEvent(event, context);
      }

      if (handlers.shouldCancel) {
        context._shouldCancel = handlers.shouldCancel(event, context);

        if (context._shouldCancel) {
          if (handlers.onEnd) handlers.onEnd(event, context, true);
          return;
        }
      }

      if (handlers.beforeEach) {
        handlers.beforeEach(event, context);
      }

      if (isBegan && handlers.onStart) {
        handlers.onStart(event, context);
      }

      if (event.state === State.ACTIVE && handlers.onActive) {
        handlers.onActive(event, context);
      }
      if (
        event.oldState === State.ACTIVE &&
        event.state === State.END &&
        handlers.onEnd
      ) {
        handlers.onEnd(event, context, false);
      }
      if (
        event.oldState === State.ACTIVE &&
        event.state === State.FAILED &&
        handlers.onFail
      ) {
        handlers.onFail(event, context);
      }
      if (
        event.oldState === State.ACTIVE &&
        event.state === State.CANCELLED &&
        handlers.onCancel
      ) {
        handlers.onCancel(event, context);
      }

      if (event.oldState === State.ACTIVE) {
        if (handlers.onFinish) {
          handlers.onFinish(
            event,
            context,
            event.state === State.CANCELLED ||
              event.state === State.FAILED,
          );
        }
      }

      if (handlers.afterEach) {
        handlers.afterEach(event, context);
      }
    }
    // clean up context
    if (event.oldState === State.ACTIVE) {
      context._shouldSkip = undefined;
      context._shouldCancel = undefined;
    }
  }, []);

  return handler;
}