多个手势响应者同时

时间:2016-05-09 21:24:05

标签: react-native

我需要一些可以同时按下的按钮,但是目前如果你按下一个按钮,它会“声称”响应,而其他按钮则无法按下。我该怎么做?

1 个答案:

答案 0 :(得分:1)

知道了。您必须使用ReactNativeEventEmitter直接收听触摸事件并完全绕过手势响应程序。下面是一个装饰器类,只要收到这些触摸事件,就会在包装类中调用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);
                    }}
                />
            );
        }
    };
}