4

我正在创建一个可拖动的框。我可以将其拖动到屏幕上的任何位置,但出现此错误,提示“您试图_value在一个本应不可变且已被冻结的对象上设置密钥”。谁能告诉我我做错了什么。

我的代码:

import React, { Component } from 'react'
import {
  AppRegistry,
  StyleSheet,
  Text,
  Button,
  ScrollView,
  Dimensions,
  PanResponder,
  Animated,
  View
} from 'react-native'

import { StackNavigator } from 'react-navigation'




export default class Home extends Component{

componentWillMount(){

  this.animatedValue = new Animated.ValueXY();

this.panResponder = PanResponder.create({

 onStartShouldSetPanResponder: (evt, gestureState) => true,
  onMoveShouldSetPanResponder: (evt, gestureState) => true,
  onPanResponderGrant: (e, gestureState) => {



      },

  onPanResponderMove:Animated.event([


null,{dx: this.animatedValue.x , dy:this.animatedValue.y}

    ]),  
  onPanResponderRelease: (e, gestureState) => { 
      },       
})
}

  render(){

    const animatedStyle = {

      transform:this.animatedValue.getTranslateTransform()
    }

    return(

        <View style={styles.container}>

        <Animated.View style={[styles.box ,animatedStyle]} {...this.panResponder.panHandlers}>


        <Text>Home</Text>


        </Animated.View>
        </View>
      )
  }
}


var styles = StyleSheet.create({
  container: {
    flex: 1,
    marginLeft: 10,
    marginRight: 10,
    alignItems: 'stretch',
    justifyContent: 'center',
  },

  box:{

height:90,
width:90,
textAlign:'center'



  }
});
4

2 回答 2

5

在我的情况下,我收到了这个错误,因为我忘记将 in 更改ViewAnimated.View.

于 2020-07-16T10:19:28.240 回答
0

试试这个。这将解决您的问题。您需要在 state 对象中初始化 animatedValue 以使其工作。

constructor(props) {
  super(props);
  this.state = {
    animatedValue: new Animated.ValueXY()
  }
}

onPanResponderMove:Animated.event([
 null,{dx: this.state.animatedValue.x , dy:this.state.animatedValue.y}
]),
于 2017-07-18T11:05:40.887 回答