Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 本机动画模块不支持样式属性“width”:重写我的代码需要建议吗_React Native_Android Animation_React Native Animatable - Fatal编程技术网

React native 本机动画模块不支持样式属性“width”:重写我的代码需要建议吗

React native 本机动画模块不支持样式属性“width”:重写我的代码需要建议吗,react-native,android-animation,react-native-animatable,React Native,Android Animation,React Native Animatable,我继承了以下组件,该组件与react native的早期版本配合良好,在其他组件显示的按钮上显示不透明的滚动进度条 最近,当我升级到react native 0.62.2时,我在请求添加useNativeDriver时出错。当添加它并将其设置为“是”时,我得到一个错误,即本机动画模块不支持样式属性“宽度” 当将useNativeDriver设置为false时,我没有得到错误,但动画不起作用,至少在android上不起作用。没有在iOS上测试 关于如何修改代码以使其与最新版本的react nati

我继承了以下组件,该组件与react native的早期版本配合良好,在其他组件显示的按钮上显示不透明的滚动进度条

最近,当我升级到react native 0.62.2时,我在请求添加useNativeDriver时出错。当添加它并将其设置为“是”时,我得到一个错误,即本机动画模块不支持样式属性“宽度”

当将useNativeDriver设置为false时,我没有得到错误,但动画不起作用,至少在android上不起作用。没有在iOS上测试

关于如何修改代码以使其与最新版本的react native一起工作,有什么建议吗

import React, { Component } from 'react';
import { Animated, Easing } from 'react-native';
import { connect } from 'react-redux';

class ProgressBar_Module extends Component {
  constructor(props) {
    super(props);

    this.state = {
      progress: 0
    }
    this.d_isMounted = false;
    this.d_initialTime = 0;
    this.d_animation_width = new Animated.Value(1);
    this.d_animation_progressTime = 3000;
  }

  componentDidMount() {
    this.d_initialTime = Date.now();
    this.d_isMounted = true;
    this.progressCount();
  }

  componentDidUpdate(prevProps) {
    if(prevProps.timerStart < this.props.timerStart) {
      this.setState({
        animation_width: new Animated.Value(1),
        animation_progressTime: 3000
      });
      this.progressCount(true)
    }
  }

  componentWillUnmount() {
    this.d_isMounted = false;
  }

  progressCount = (reset) => {
    if( reset ) {
      this.d_animation_width = new Animated.Value(1);
      this.d_animation_progressTime = 3000;
    }
    Animated.timing(this.d_animation_width, {
      toValue: 175,
      duration: this.d_animation_progressTime,
      easing: Easing.linear
    }).start(() => {
      if(this.props.timer && this.d_animation_width.__getValue() === 175) {
        this.props.onPress();
      }
    })
  }

  render() {

    return (
      <Animated.View
        style={[
          {
            position: 'absolute',
            left: 0,
            height: '150%',
            width: 0,
            zIndex: 1,
            backgroundColor: 'black',
          },
          { width: this.props.timer !== false ?
              this.d_animation_width : 175 }
        ]}
        onPress={this.props.onPress}
      >
      </Animated.View>
    )
  }
}

const mapStateToProps = state => {
  return {
    timer: state.get('ui').get('timer').get('timer'),
    reset: state.get('ui').get('resetTimer').get('resetTimer'),
    timerStart: state.get('ui').get('resetTimer').get('timerStart')
  }
};

export const ProgressBar = connect(mapStateToProps)(ProgressBar_Module);

尝试使用transform和scaleX,而不是设置宽度动画。 以下是一些有关react本机转换的参考:
尝试使用transform和scaleX,而不是设置宽度动画。 以下是一些有关react本机转换的参考:
根据@YaNuSH的建议,我只需要更换

   width: animatedValue
与:

详情如下: 之前: 之后: 之前: 之后:
按照@YaNuSH的建议,我只需要更换

   width: animatedValue
与:

详情如下: 之前: 之后: 之前: 之后: 试着这样做

useNativeDriver: false
这对我很有效。

试着这样做

useNativeDriver: false

这对我很有效。

这确实是一条路要走。我发布了另一个详细的答案。如果初始宽度和高度从0开始,我该怎么办?@SureshMurali,我认为最好将初始宽度设置为应用程序中可以获得的最大值。这样,当您使用transform时,您知道scaleX:0将是宽度0,scaleX:0.5将是宽度/2等。但根据我的设计,我希望通过设置宽度:0使其不可见。还有其他解决方案吗?我不明白为什么不能将scaleX设置为0,而不是宽度设置为0。这确实是一种方法。我发布了另一个详细的答案。如果初始宽度和高度从0开始,我该怎么办?@SureshMurali,我认为最好将初始宽度设置为应用程序中可以获得的最大值。这样,当您使用transform时,您知道scaleX:0将是宽度0,scaleX:0.5将是宽度/2等。但根据我的设计,我希望通过设置宽度:0使其不可见。还有其他解决方案吗?我不明白为什么不能将scaleX设置为0而不是宽度设置为0
return (
      <Animated.View
        style={[{
          position: 'absolute',
          left: 0,
          height: '150%',
          width: 0,
          zIndex: 1,
          backgroundColor: 'black',
        }, 
        {
          width: this.props.timer !== false ?
             this.d_animation_width : 175
        }]}
        onPress={this.props.onPress}
      >
      </Animated.View>
    )
const scaling = this.props.timer !== false ? this.d_animation_width : 175;

return (
  <Animated.View
    style={[
      {
        position: 'absolute',
        left: 0,
        height: '150%',
        width: 2,
        zIndex: 1,
        backgroundColor: 'black',
      },
      {
        transform: [
          {
            scaleX: scaling
          },
        ],
      },
    ]}
    onPress={this.props.onPress}
  >
  </Animated.View>
)
useNativeDriver: false