【发布时间】:2017-03-29 13:53:34
【问题描述】:
我对 React Native 很陌生,但遇到了一个问题:
我了解您可以使用 PanResponder 然后使用 interpolate 函数进行的转换。
例子:
let cardOpacity = {opacity: this.state.pan.x.interpolate({inputRange: [-150, 0, 150], outputRange: [0.9, 0, 0.9]})};
我面临的问题有点棘手,因为我想从这个插值中提取值而不是任何样式:
我希望该代码基本上可以工作:
_renderApplyOrDislikeHint() {
let cardOpacity = {opacity: this.state.pan.x.interpolate({inputRange: [-150, 0, 150], outputRange: [0.9, 0, 0.9]})};
let value = {opacity: this.state.pan.x.interpolate({inputRange: [-150, 0, 150], outputRange: [1, 0, 1]})};
this.state.pan.x.addListener(({value}) => {
this.x = value._value
console.log('Value changed', this.x);
});
let dimensionStyle = {
backgroundColor: 'white',
width: this.props.cardWidth,
height: this.props.cardHeight,
position: 'absolute'
};
return (
<Animated.View style={[dimensionStyle, cardOpacity]}>
{this.renderHint()}
</Animated.View>
)
}
renderHint(){
console.log('X in render hint', this.x)
return this.x > 0 ? <Text>Yes</Text> : <Text>Nope</Text>
}
问题是动画值非常适合样式,但它们不会强制重新渲染 renderHint() 函数。
在渲染过程中改变状态是一个非常糟糕的主意。
我怎样才能做到这一点?
【问题讨论】:
标签: javascript react-native jsx