Search in sources :

Example 1 with GestureHandler

use of com.swmansion.gesturehandler.GestureHandler in project react-native-gesture-handler by kmagiera.

the class RNGestureHandlerModule method createGestureHandler.

@ReactMethod
public void createGestureHandler(String handlerName, int handlerTag, ReadableMap config) {
    for (int i = 0; i < mHandlerFactories.length; i++) {
        HandlerFactory handlerFactory = mHandlerFactories[i];
        if (handlerFactory.getName().equals(handlerName)) {
            GestureHandler handler = handlerFactory.create(getReactApplicationContext());
            handler.setTag(handlerTag);
            handler.setOnTouchEventListener(mEventListener);
            mRegistry.registerHandler(handler);
            mInteractionManager.configureInteractions(handler, config);
            handlerFactory.configure(handler, config);
            return;
        }
    }
    throw new JSApplicationIllegalArgumentException("Invalid handler name " + handlerName);
}
Also used : TapGestureHandler(com.swmansion.gesturehandler.TapGestureHandler) GestureHandler(com.swmansion.gesturehandler.GestureHandler) PinchGestureHandler(com.swmansion.gesturehandler.PinchGestureHandler) PanGestureHandler(com.swmansion.gesturehandler.PanGestureHandler) RotationGestureHandler(com.swmansion.gesturehandler.RotationGestureHandler) LongPressGestureHandler(com.swmansion.gesturehandler.LongPressGestureHandler) NativeViewGestureHandler(com.swmansion.gesturehandler.NativeViewGestureHandler) JSApplicationIllegalArgumentException(com.facebook.react.bridge.JSApplicationIllegalArgumentException) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 2 with GestureHandler

use of com.swmansion.gesturehandler.GestureHandler in project react-native-gesture-handler by kmagiera.

the class RNGestureHandlerRegistry method dropHandler.

public synchronized void dropHandler(int handlerTag) {
    GestureHandler handler = mHandlers.get(handlerTag);
    if (handler != null) {
        detachHandler(handler);
        mHandlers.remove(handlerTag);
    }
}
Also used : GestureHandler(com.swmansion.gesturehandler.GestureHandler)

Example 3 with GestureHandler

use of com.swmansion.gesturehandler.GestureHandler in project react-native-gesture-handler by kmagiera.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wrapper = (GestureHandlerViewWrapper) findViewById(R.id.wrapper);
    scrollView = (ScrollView) findViewById(R.id.scroll);
    button = (Button) findViewById(R.id.button);
    seekBar = (SeekBar) findViewById(R.id.seekbar);
    block = findViewById(R.id.block);
    largeBlock = findViewById(R.id.large_block);
    switchView = (Switch) findViewById(R.id.switchView);
    // Native click events should work as expected assuming the view is wrapped with
    // NativeViewGestureHandler
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "I'm touched", Toast.LENGTH_SHORT).show();
        }
    });
    GestureHandlerRegistryImpl registry = wrapper.getRegistry();
    registry.registerHandlerForView(scrollView, new NativeViewGestureHandler());
    registry.registerHandlerForView(button, new NativeViewGestureHandler());
    registry.registerHandlerForView(seekBar, new NativeViewGestureHandler()).setDisallowInterruption(true).setShouldActivateOnStart(true).setShouldCancelWhenOutside(false);
    registry.registerHandlerForView(switchView, new NativeViewGestureHandler()).setShouldActivateOnStart(true).setDisallowInterruption(true).setShouldCancelWhenOutside(false).setHitSlop(20);
    registry.registerHandlerForView(block, new LongPressGestureHandler()).setOnTouchEventListener(new OnTouchEventListener<LongPressGestureHandler>() {

        @Override
        public void onTouchEvent(LongPressGestureHandler handler, MotionEvent event) {
        }

        @Override
        public void onStateChange(LongPressGestureHandler handler, int newState, int oldState) {
            if (newState == GestureHandler.STATE_ACTIVE) {
                Toast.makeText(MainActivity.this, "Long press", Toast.LENGTH_SHORT).show();
            }
        }
    });
    registry.registerHandlerForView(block, new TapGestureHandler()).setNumberOfTaps(2).setOnTouchEventListener(new OnTouchEventListener<TapGestureHandler>() {

        @Override
        public void onTouchEvent(TapGestureHandler handler, MotionEvent event) {
        }

        @Override
        public void onStateChange(TapGestureHandler handler, int newState, int oldState) {
            if (newState == GestureHandler.STATE_ACTIVE) {
                Toast.makeText(MainActivity.this, "I'm d0able tapped", Toast.LENGTH_SHORT).show();
            }
        }
    });
    registry.registerHandlerForView(block, new TapGestureHandler()).setNumberOfTaps(1).setOnTouchEventListener(new OnTouchEventListener<TapGestureHandler>() {

        @Override
        public void onTouchEvent(TapGestureHandler handler, MotionEvent event) {
        }

        @Override
        public void onStateChange(TapGestureHandler handler, int newState, int oldState) {
            if (newState == GestureHandler.STATE_ACTIVE) {
                Toast.makeText(MainActivity.this, "I'm tapped once", Toast.LENGTH_SHORT).show();
            }
        }
    });
    GestureHandlerInteractionController pinchAndRotateInteractionController = new BaseGestureHandlerInteractionController() {

        @Override
        public boolean shouldRecognizeSimultaneously(GestureHandler handler, GestureHandler otherHandler) {
            // Allow pinch and rotate handlers registered for largeBlock to run simultaneously
            return handler.getView().equals(largeBlock) && handler instanceof PinchGestureHandler;
        }
    };
    registry.registerHandlerForView(largeBlock, new RotationGestureHandler()).setInteractionController(pinchAndRotateInteractionController).setOnTouchEventListener(new OnTouchEventListener<RotationGestureHandler>() {

        private double mRotation = 0f;

        @Override
        public void onTouchEvent(RotationGestureHandler handler, MotionEvent event) {
            if (handler.getState() == GestureHandler.STATE_ACTIVE) {
                largeBlock.setRotation((float) Math.toDegrees(mRotation + handler.getRotation()));
            }
        }

        @Override
        public void onStateChange(RotationGestureHandler handler, int newState, int oldState) {
            if (oldState == GestureHandler.STATE_ACTIVE) {
                mRotation += handler.getRotation();
            }
        }
    });
    registry.registerHandlerForView(largeBlock, new PinchGestureHandler()).setOnTouchEventListener(new OnTouchEventListener<PinchGestureHandler>() {

        private double mScale = 1f;

        @Override
        public void onTouchEvent(PinchGestureHandler handler, MotionEvent event) {
            if (handler.getState() == GestureHandler.STATE_ACTIVE) {
                largeBlock.setScaleX((float) (mScale * handler.getScale()));
                largeBlock.setScaleY((float) (mScale * handler.getScale()));
            }
        }

        @Override
        public void onStateChange(PinchGestureHandler handler, int newState, int oldState) {
            if (oldState == GestureHandler.STATE_ACTIVE) {
                mScale *= handler.getScale();
            }
        }
    });
    registry.registerHandlerForView(largeBlock, new PanGestureHandler(this)).setMinDy(2).setMaxPointers(1).setOnTouchEventListener(new OnTouchEventListener<PanGestureHandler>() {

        @Override
        public void onTouchEvent(PanGestureHandler handler, MotionEvent event) {
            if (handler.getState() == GestureHandler.STATE_ACTIVE) {
                largeBlock.setTranslationX(handler.getTranslationX());
                largeBlock.setTranslationY(handler.getTranslationY());
            }
        }

        @Override
        public void onStateChange(PanGestureHandler handler, int newState, int oldState) {
        }
    });
}
Also used : TapGestureHandler(com.swmansion.gesturehandler.TapGestureHandler) GestureHandlerInteractionController(com.swmansion.gesturehandler.GestureHandlerInteractionController) BaseGestureHandlerInteractionController(com.swmansion.gesturehandler.BaseGestureHandlerInteractionController) GestureHandlerRegistryImpl(com.swmansion.gesturehandler.GestureHandlerRegistryImpl) PanGestureHandler(com.swmansion.gesturehandler.PanGestureHandler) RotationGestureHandler(com.swmansion.gesturehandler.RotationGestureHandler) LongPressGestureHandler(com.swmansion.gesturehandler.LongPressGestureHandler) TapGestureHandler(com.swmansion.gesturehandler.TapGestureHandler) GestureHandler(com.swmansion.gesturehandler.GestureHandler) NativeViewGestureHandler(com.swmansion.gesturehandler.NativeViewGestureHandler) PinchGestureHandler(com.swmansion.gesturehandler.PinchGestureHandler) LongPressGestureHandler(com.swmansion.gesturehandler.LongPressGestureHandler) ScrollView(android.widget.ScrollView) View(android.view.View) MotionEvent(android.view.MotionEvent) NativeViewGestureHandler(com.swmansion.gesturehandler.NativeViewGestureHandler) BaseGestureHandlerInteractionController(com.swmansion.gesturehandler.BaseGestureHandlerInteractionController) PanGestureHandler(com.swmansion.gesturehandler.PanGestureHandler) PinchGestureHandler(com.swmansion.gesturehandler.PinchGestureHandler) RotationGestureHandler(com.swmansion.gesturehandler.RotationGestureHandler)

Example 4 with GestureHandler

use of com.swmansion.gesturehandler.GestureHandler in project react-native-gesture-handler by kmagiera.

the class RNGestureHandlerModule method updateGestureHandler.

@ReactMethod
public void updateGestureHandler(int handlerTag, ReadableMap config) {
    GestureHandler handler = mRegistry.getHandler(handlerTag);
    if (handler != null) {
        HandlerFactory factory = findFactoryForHandler(handler);
        if (factory != null) {
            mInteractionManager.dropRelationsForHandlerWithTag(handlerTag);
            mInteractionManager.configureInteractions(handler, config);
            factory.configure(handler, config);
        }
    }
}
Also used : TapGestureHandler(com.swmansion.gesturehandler.TapGestureHandler) GestureHandler(com.swmansion.gesturehandler.GestureHandler) PinchGestureHandler(com.swmansion.gesturehandler.PinchGestureHandler) PanGestureHandler(com.swmansion.gesturehandler.PanGestureHandler) RotationGestureHandler(com.swmansion.gesturehandler.RotationGestureHandler) LongPressGestureHandler(com.swmansion.gesturehandler.LongPressGestureHandler) NativeViewGestureHandler(com.swmansion.gesturehandler.NativeViewGestureHandler) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 5 with GestureHandler

use of com.swmansion.gesturehandler.GestureHandler in project react-native-gesture-handler by kmagiera.

the class RNGestureHandlerRegistry method attachHandlerToView.

public synchronized boolean attachHandlerToView(int handlerTag, int viewTag) {
    GestureHandler handler = mHandlers.get(handlerTag);
    if (handler != null) {
        detachHandler(handler);
        registerHandlerForViewWithTag(viewTag, handler);
        return true;
    } else {
        return false;
    }
}
Also used : GestureHandler(com.swmansion.gesturehandler.GestureHandler)

Aggregations

GestureHandler (com.swmansion.gesturehandler.GestureHandler)5 LongPressGestureHandler (com.swmansion.gesturehandler.LongPressGestureHandler)3 NativeViewGestureHandler (com.swmansion.gesturehandler.NativeViewGestureHandler)3 PanGestureHandler (com.swmansion.gesturehandler.PanGestureHandler)3 PinchGestureHandler (com.swmansion.gesturehandler.PinchGestureHandler)3 RotationGestureHandler (com.swmansion.gesturehandler.RotationGestureHandler)3 TapGestureHandler (com.swmansion.gesturehandler.TapGestureHandler)3 ReactMethod (com.facebook.react.bridge.ReactMethod)2 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 ScrollView (android.widget.ScrollView)1 JSApplicationIllegalArgumentException (com.facebook.react.bridge.JSApplicationIllegalArgumentException)1 BaseGestureHandlerInteractionController (com.swmansion.gesturehandler.BaseGestureHandlerInteractionController)1 GestureHandlerInteractionController (com.swmansion.gesturehandler.GestureHandlerInteractionController)1 GestureHandlerRegistryImpl (com.swmansion.gesturehandler.GestureHandlerRegistryImpl)1