【问题标题】:How to change React Native Animated sequence timings on receiving props如何在接收道具时更改 React Native 动画序列时间
【发布时间】:2018-10-22 16:03:22
【问题描述】:

我有一个循环动画,我希望用户能够更改其时间。我使用 redux 进行了设置,以便当用户在设置中更改它们时,被动画的组件接收新的计时作为道具。但是这不会改变序列的时间(我猜这是因为序列分配了变量的值,而不是引用改变的变量的值)

class BreatheCircle extends React.Component {
      state = {
            circleAnimation: new Animated.Value(0.6),
            holdAnimation: new Animated.Value(0),
            inhaleAnimation: new Animated.Value(0),
            exhaleAnimation: new Animated.Value(0),
        }
        componentWillReceiveProps(nextProps){
          console.log('next props:', nextProps)
        }

breathAnimation = 
    Animated.loop(
      Animated.sequence([
      Animated.timing(this.state.inhaleAnimation, {toValue: 1, duration:10}),
      Animated.timing(this.state.circleAnimation, {toValue: 1, duration:this.props.in*1000}),
      Animated.timing(this.state.inhaleAnimation, {toValue: 0, duration:10}),
      Animated.timing(this.state.holdAnimation, {toValue: 1, duration:10}),
      Animated.timing(this.state.holdAnimation, {toValue: 0, duration:10, delay: this.props.hold*1000}), //delay for the hold to disappear
      Animated.timing(this.state.exhaleAnimation, {toValue: 1, duration:10}),
      Animated.timing(this.state.circleAnimation, {toValue: 0.6, duration:this.props.out*1000}),
      Animated.timing(this.state.exhaleAnimation, {toValue: 0, duration:10}),
]))


componentDidMount(){
  // this.state.animated.setValue(0.6) // state already declare
  this.animateCircle();
}
componentWillUnmount(){
  this.breathAnimation.stop();
}

animateCircle(){
    this.breathAnimation.start();
}


  render(){
    const{animated} = this.state;
    return(
        <View style={{alignItems: 'center'}}>
        <Animated.View style={{
          width: 350,
          height: 350,
          borderRadius: 200,
          transform: [ {scale: this.state.circleAnimation} ]
        }}>
          <LinearGradient colors={['rgb(255, 206, 224)', 'rgb(248, 120, 131)']} style={{ alignItems: 'center', justifyContent: 'center', flex: 1, borderRadius: 200}}>
            <Animated.Text style={{
              position: 'absolute',
              justifyContent: 'center',
              fontSize: 80,
              fontWeight: 'bold',
              color: 'white',
              opacity: this.state.holdAnimation.interpolate({
                inputRange: [0, 1],
                outputRange: [0, 1],
                extrapolate: 'clamp',
                                },)
                              }}>Hold</Animated.Text>
            <Animated.Text style={{
              position: 'absolute',
              justifyContent: 'center',
              fontSize: 80,
              fontWeight: 'bold',
              color: 'white',
              opacity: this.state.inhaleAnimation.interpolate({
                inputRange: [0, 1],
                outputRange: [0, 1],
                extrapolate: 'clamp',
                                },)
                              }}>Inhale</Animated.Text>
            <Animated.Text style={{
              position: 'absolute',
              justifyContent: 'center',
              fontSize: 80,
              fontWeight: 'bold',
              color: 'white',
              opacity: this.state.exhaleAnimation.interpolate({
                inputRange: [0, 1],
                outputRange: [0, 1],
                extrapolate: 'clamp',
                                },)
                              }}>Exhale</Animated.Text>
          </LinearGradient>
        </Animated.View>
      </View>
    )
  }
}

const mapStateToProps = state => {
  return {
    in: state.in,
    hold: state.hold,
    out: state.out,
  };
};

  export default connect(
    mapStateToProps
  )(BreatheCircle);

当新的道具传递给它时,我将如何改变时间?

【问题讨论】:

    标签: reactjs react-native react-redux react-animated react-animations


    【解决方案1】:

    我建议你使用生命周期方法componentDidUpdate:如果你的mapStateToProps工作正常,你可以像这样简单地更新组件的状态:

    componentDidUpdate(previousProps, previousState) {
      if (this.props !== previousProps){
        this.setState({
          ... // update your component state with the props you passed here
        })
      }
    }
    

    这里是doc

    【讨论】:

    • 只是为了澄清如何设置状态:this.setState({ holdAnimation: this.props.hold }); 或任何你想要的
    • 我已经尝试过了,但是状态的更新并没有更新动画。我正在尝试更改动画的持续时间,而不是 Animated 值,因此我需要以某种方式使用新的时间重新启动动画,但到目前为止我尝试过的方法都没有奏效。
    猜你喜欢
    • 2019-01-31
    • 2017-09-28
    • 2018-01-10
    • 1970-01-01
    • 2021-11-25
    • 2018-05-23
    • 2020-10-16
    • 2021-01-06
    • 2018-11-20
    相关资源
    最近更新 更多