React Native - LayoutAnimation:如何让它只在组件内动画 object,而不是整个 component/view?

React Native - LayoutAnimation: how to make it just animate object inside component, not whole component/view?

我正在尝试遵循此 example (code here) 并在我的 RN 项目中使用 LayoutAnimation(与例如,我只想渲染我的圈子而不需要按下任何按钮)。

但是,当我添加 LayoutAnimation 时,整个 view/screen/component 都会执行 'springing in' 的动画,而不仅仅是我想要的圆圈 。我必须将 LayoutAnimation 移动到哪里才能实现仅对圆圈 objects 进行动画处理?

再次更新:听从了 bennygenel 的建议,制作了一个单独的 Circles 组件,然后在收藏夹中,有一个 componentDidMount 可以添加每个 Cricle逐个组件,随着状态的更新时间延迟,导致单独的动画。但是我还是没有得到圆圈的预期效果 rendering/animating 一个一个...

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>
    );
  }
}

来自 configureNext() 文档;

static configureNext(config, onAnimationDidEnd?)

Schedules an animation to happen on the next layout.

这意味着您需要在呈现要设置动画的组件之前配置 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>
    );
  }
}

更新

如果您想使用 Circle 组件作为您的示例,您需要像下面那样使用它,这样子组件也可以被渲染。更详细的解释可以参考here.

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

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