我正在尝试实现我自己的react原生swiper组件,但是当在Animated.View中添加TouchableOpacity时,swiper立即停止工作。我认为触摸屏将成为响应器,而我自己的panResponder将无法工作。(您可以看到按压的不透明性)
问题是,我想让触摸屏工作,但如果屏幕上的触摸不是一次点击,而是拖动,那么我想让触摸屏工作。(将scrollView与触摸屏一起使用时,此行为有效)
这里:Make TouchableOpacity not highlight element when starting to scroll [React Native]说:“滚动手势应该取消TouchableOpacity触摸响应器”,但这是如何做到的呢?
出于测试目的,我尝试使用以下代码捕获Animated.View的响应者:
onStartShouldSetResponderCapture: () => true,但它似乎没有效果(我原以为用这个可以滑动,但触摸屏却不行)。
以下是示例代码(如果您将TouchableOpacity更改为查看滑动正在工作):
import React, { Component } from 'react';
import { View, Animated, PanResponder, Dimensions, TouchableOpacity, Text } from 'react-native';
class App extends Component {
componentWillMount() {
const position = new Animated.ValueXY();
const panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (event, gesture) => {
position.setValue({ x: gesture.dx, y: 0 })
},
onPanResponderRelease: (event, gesture) => {
Animated.spring(this.state.position, {
toValue: { x: 0, y: 0 }
}).start();
}
});
this.state = { panResponder, position };
}
render() {
return (
<Animated.View style={[this.state.position.getLayout(), styles.viewStyle]} {...this.state.panResponder.panHandlers}>
<TouchableOpacity style={styles.touchableStyle}>
<Text>Swipe not working</Text>
</TouchableOpacity>
</Animated.View>
);
}
}
const styles = {
viewStyle: {
position: 'absolute',
top: 0,
bottom: 0,
width: Dimensions.get('window').width
},
touchableStyle: {
backgroundColor: '#ddd',
height: 100,
borderWidth: 5,
borderColor: '#000',
justifyContent: 'center',
alignItems: 'center'
}
};
export default App;如果你有解决方案,请帮帮我。我已经试着让它工作了一段时间了。
发布于 2017-09-14 04:01:28
它设法解决了这个问题:
onStartShouldSetPanResponder: () => true必须替换为:
onMoveShouldSetPanResponder: () => true不透明高光仍然存在同样的问题,但这不是那么大的问题。Make TouchableOpacity not highlight element when starting to scroll [React Native]
https://stackoverflow.com/questions/46200549
复制相似问题