React Native - LayoutAnimation:如何使它成为组件内的动画对象,而不是整个组件/视图?

Spi*_*uce 7 javascript layout-animation reactjs react-native

我正在尝试按照这个例子(这里是代码)并在我的RN项目中使用LayoutAnimation(与该示例的不同之处在于我只想渲染我的圈子而没有按下按钮).

但是当我添加了LayoutAnimation时,它就是整个视图/屏幕/组件,它可以完成"弹入"的动画,而不仅仅是我想要的圆圈.我必须在哪里移动LayoutAnimation才能实现动画的圆形对象?

再次注意:Heed bennygenel建议制作一个单独的Circles组件然后在Favorites上,有一个componentDidMount会逐个添加每个Cricle组件,导致个别动画,因为状态会随着时间延迟而更新.但是我仍然没有逐一获得圆形渲染/动画的预期效果......

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (
        <View>
          { this.props.children }
        </View>
    );
  }
}

class Favorites extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      circleCount: 0
    }
  }
  componentDidMount() {
    for(let i = 0; i <= this.props.screenProps.appstate.length; i++) {
      setTimeout(() => {
        this.addCircle();
      }, (i*500));
    }
  }
  addCircle = () => {
    this.setState((prevState) => ({circleCount: prevState.circleCount + 1}));
  }

render() {
    var favoritesList = this.props.screenProps.appstate;

    circles = favoritesList.map((item) => {
        return (
            <Circle key={item.url} style={styles.testcontainer}>
              <TouchableOpacity onPress={() => {
                  Alert.alert( "Add to cart and checkout?",
                              item.item_name + "? Yum!",
                              [
                                {text: 'Yes', onPress: () => console.log(item.cust_id)},
                                {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
                              ]
                              )}}>
                <Image source={{uri: item.url}} />
               </TouchableOpacity>
            </Circle>
        )});

    return (
        <ScrollView}>
          <View>
            <View>
              {circles}
            </View>
          </View>
        </ScrollView>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

ben*_*nel 6

来自configureNext()文档;

static configureNext(config, onAnimationDidEnd?)

安排在下一个布局上发生的动画。

这意味着您需要LayoutAnimation在渲染要动画的组件之前进行配置。如果您将Circle组件分开并LayoutAnimation为该组件设置

例子

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (<View style={{width: 50, height: 50, backgroundColor: 'red', margin: 10, borderRadius: 25}}/>);
  }
}

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      circleCount: 0
    }
  }
  componentDidMount() {
    for(let i = 0; i < 4; i++) {
      setTimeout(() => {
        this.addCircle();
      }, (i*200));
    }
  }
  addCircle = () => {
    this.setState((prevState) => ({circleCount: prevState.circleCount + 1}));    
  }
  render() {
    var circles = [];
    for (var i = 0; i < this.state.circleCount; i++) {
      circles.push(<Circle />);
    }
    return (
    <View>
      <View style={{flexDirection:'row', justifyContent:'center', alignItems: 'center', marginTop: 100}}>
        { circles }
      </View>
      <Button color="blue" title="Add Circle" onPress={this.addCircle} />
    </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

更新

如果你想使用Circle组件作为你的例子,你需要像下面一样使用它,这样子组件也可以被渲染。更详细的解释可以在这里找到。

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (
        <View>
          { this.props.children }
        </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)