简体   繁体   English

React Native PanResponder 不适用于 Android 设备

[英]React Native PanResponder not working on Android device

For some reason, I cannot make the PanResponder on my Android device.出于某种原因,我无法在我的 Android 设备上制作 PanResponder。 I use a Motorola Moto E (2nd gen) with Android 5.0.2我使用带有 Android 5.0.2 的摩托罗拉 Moto E(第 2 代)

I was able to make this work on Android emulators (Nexus 6: Android 5.1 and Nexus S: Android 4.4) and on iOS Simulator and Device.我能够在 Android 模拟器(Nexus 6:Android 5.1 和 Nexus S:Android 4.4)以及 iOS 模拟器和设备上完成这项工作。

Any idea why the PanResponder doesn't work on device?知道为什么PanResponder在设备上不起作用吗?

Some code here just in case it's needed.这里有一些代码以防万一。 None of the console.log works on the device:没有任何console.log在设备上工作:

componentWillMount() {
    this.panResponder = this.getPanResponder();
}

getPanResponder() {
    return PanResponder.create({
        onStartShouldSetPanResponder: () => {
            console.log('start');
            return true;
        },
        onStartShouldSetPanResponderCapture: () => {
            console.log('start capture');
            return true;
        },
        onMoveShouldSetPanResponder: () => {
            console.log('move');
            return true;
        },
        onMoveShouldSetPanResponderCapture: () => {
            console.log('move capture');
            return true;
        },
        onPanResponderMove: () => {
            console.log('moving');
        }
     })
}

render() {
    return (
        <View
            style={styles.container}
            {...this.panResponder.panHandlers}
        >
        </View>
    )
}

Thanks谢谢

I had the same problem, and in my case what solved the problem is setting a backgroundColor to the view with the pan responder handlers.我遇到了同样的问题,在我的情况下,解决问题的方法是使用 pan 响应程序处理程序为视图设置 backgroundColor。 If you can't make it colorful (as I couldn't), just set the backgroundColor to something unnoticeable, like rgba(255,255,255,0.01)如果你不能让它丰富多彩(因为我不能),只需将 backgroundColor 设置为不明显的东西,比如rgba(255,255,255,0.01)

It seems that android ignores pan responder handlers on transparent views似乎 android 忽略了透明视图上的 pan 响应程序处理程序

It seems, onResponder... callbacks only works after grant current view as responder, ie you should return true from one of these callbacks (dependent on gesture type and event phase): onStartShouldSetResponder, onStartShouldSetResponderCapture, onMoveShouldSetResponder, onMoveShouldSetResponderCapture.看来,onResponder... 回调仅在授予当前视图作为响应者后才有效,即您应该从以下回调之一(取决于手势类型和事件阶段)返回 true:onStartShouldSetResponder、onStartShouldSetResponderCapture、onMoveShouldSetResponder、onMoveShouldSetResponderCapture。 In my opinion this is a quite correct behaviour.在我看来,这是一个非常正确的行为。

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

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