在 React Native Animated 中使用 useNativeDriver 时出错

Mah*_*our 2 animation react-native react-animated

当我useNativeDriver在 React Native Animated 中使用时

state = {
    chevronUp: new Animated.Value(-50),
};

Animated.spring(this.state.chevronUp, {
    toValue: 50,
    friction: 5,
    useNativeDriver: true,  // <----- this line
}).start();
Run Code Online (Sandbox Code Playgroud)

并渲染

<Animated.View style={{bottom: this.state.chevronUp,position: "absolute", right: 20, width: 50, height: 50}}>
     <Icon name="chevron-up" size={28} color="#666"/>
</Animated.View>
Run Code Online (Sandbox Code Playgroud)

这些错误给了我

原生动画模块不支持样式属性“底部”

无法在未安装的组件上调用 setState(或 forceUpdate)。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 componentWillUnmount 方法中的所有订阅和异步任务。

小智 6

您需要使用"translateY"属性而不是"bottom"本机驱动程序支持的属性,因此您的初始值将如下所示:

state = {
    chevronUp: new Animated.Value(50),
}

Animated.spring(this.state.chevronUp, {
    toValue: -50,
    friction: 5,
    useNativeDriver: true,  // <----- this line
}).start();
Run Code Online (Sandbox Code Playgroud)

并在渲染方法中:

<Animated.View style={{translateY: this.state.chevronUp,position: "absolute", right: 20, width: 50, height: 50}}>
     <Icon name="chevron-up" size={28} color="#666"/>
</Animated.View>
Run Code Online (Sandbox Code Playgroud)