简体   繁体   English

多个手势响应者同时

[英]Multiple gesture responders at the same time

I need some buttons that can be pressed at the same time, but currently if you press one, it 'claims' responsiveness and the others can't be pressed anymore. 我需要一些可以同时按下的按钮,但是目前如果按下一个按钮,它“声称”响应性,其他按钮不能再被按下了。 How do I do this? 我该怎么做呢?

Got it. 得到它了。 You have to use ReactNativeEventEmitter to directly listen to touch events and bypass the Gesture Responder stuff entirely. 您必须使用ReactNativeEventEmitter直接监听触摸事件并完全绕过手势响应程序。 Below is a decorator class that calls onTouchStart , onTouchEnd and onTouchMove in the wrapped class whenever those touch events are received. 下面是一个装饰器类,只要收到这些触摸事件, onTouchEnd onTouchMove在包装类中调用onTouchStartonTouchEndonTouchMove

'use strict';

import React, {Component} from 'react-native';
import ReactNativeEventEmitter from 'ReactNativeEventEmitter';
import NodeHandle from 'NodeHandle';

export const multitouchable = BaseComponent => {
    return class extends Component {

        constructor(props, context) {
            super(props, context);

            this.comp = null;
            this.compId = null;
        }

        componentDidMount() {
            if(this.comp && this.compId){
                this.comp.onTouchStart && ReactNativeEventEmitter.putListener(this.compId, 'onTouchStart', e => this.comp.onTouchStart(e));
                this.comp.onTouchEnd && ReactNativeEventEmitter.putListener(this.compId, 'onTouchEnd', e => this.comp.onTouchEnd(e));
                this.comp.onTouchMove && ReactNativeEventEmitter.putListener(this.compId, 'onTouchMove', e => this.comp.onTouchMove(e));
            }
        }

        componentWillUnmount() { 
            if(this.comp && this.compId){
                this.comp.onTouchStart && ReactNativeEventEmitter.deleteListener(this.compId, 'onTouchStart');
                this.comp.onTouchEnd && ReactNativeEventEmitter.deleteListener(this.compId, 'onTouchEnd');
                this.comp.onTouchMove && ReactNativeEventEmitter.deleteListener(this.compId, 'onTouchMove');
            }
        }

        render() {
            return (
                <BaseComponent {...this.props} {...this.state}
                    ref={c => {
                        this.comp = c;
                        const handle = React.findNodeHandle(c);
                        if(handle)
                            this.compId = NodeHandle.getRootNodeID(handle);
                    }}
                />
            );
        }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM