所以,这就是我想要的,我希望图像(Animated.Image)可以移动,或者可以说我必须能够在屏幕上拖动元素,为此我将Animated.Image与PanResponder一起使用了,但这似乎不起作用。我已经阅读了博客上的所有内容,而这只是我要做的一切,但是我认为我缺少一些东西。有人可以在这方面帮助我吗?我只希望屏幕上拖动的Animated.Image。
this.state ={
pan: new Animated.ValueXY()
}
_panResponder = PanResponder.create({
// Ask to be the responder:
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
// starting point!
},
onPanResponderMove: (evt, gestureState) => {
Animated.event([
null, { dx: this.state.pan.x, dy: this.state.pan.y }
])
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
},
onPanResponderTerminate: (evt, gestureState) => {
},
onShouldBlockNativeResponder: (evt, gestureState) => {
return true;
}
});
render(){
const panStyle = {
transform: this.state.pan.getTranslateTransform()
}
const customImageStyle ={
height: 64,
width:64,
position: 'absolute'
}
return(
<Container style={{flexDirection: 'column', flex:1, marginTop: 8, marginLeft:8, marginRight:8, flex:1}} >
<Image source= {require('./image1.jpg')} style={{width: '100%', borderWidth: 5, borderColor: '#000', height:null, flex: 0.8 }} resizeMode="contain" />
<AddedImageFeatures openEmoji={this.selectEmoji} navigation = {this.props.navigation} image={require('./image1.jpg')} />
{
this.state.emojiSelected ?
<EmojiSelector chooseEmoji ={this.chooseEmoji} closeEmoji={this.closeEmoji} modalVisible = {this.state.emojiSelected} />
:
null
}
{
this.state.image ?
<Animated.Image {...this._panResponder.panHandlers} source={this.state.image} style={[customImageStyle,panStyle]}
/>
:
null
}
</Container>
)
}