父子组件的 React-Native 并行动画

标签 react-native

我有一个平行动画如下。

Animated.parallel(
[
    Animated.timing(this.translateAnimatedValue, {
        toValue: 100,
        duration: 500,
        useNativeDriver: Platform.OS == 'android'
    }),
    Animated.timing(this.opacityAnimatedValue, {
        toValue: 1,
        duration: 500,
        useNativeDriver: Platform.OS == 'android'
    })
])
.start();

它适用于我的组件。

return (
    <Animated.View style={{ opacity: this.opacityAnimatedValue, transform: [{ translateY: this.translateAnimatedValue }] }}>
        <View>
            {children}
        </View>
    </Animated.View>
);

但是我不希望父组件消失。只需要隐藏 child 。因此,当我将代码更改为以下内容时,应用程序崩溃了。

return (
    <Animated.View style={{ transform: [{ translateY: this.translateAnimatedValue }] }}>
        <View style={{ opacity: this.opacityAnimatedValue }}>
            {children}
        </View>
    </Animated.View>
);

我也把动画分开试过了。但是,它也会使应用程序崩溃

Animated.timing(this.translateAnimatedValue, {
    toValue: 100,
    duration: 500,
    useNativeDriver: Platform.OS == 'android'
}).start();

Animated.timing(this.opacityAnimatedValue, {
    toValue: 1,
    duration: 500,
    useNativeDriver: Platform.OS == 'android'
}).start();

我希望容器进行翻译并且文本出现/消失。 如何实现这种行为?

最佳答案

您忘记添加 Animated. 前缀到您的 View

替换这个:

 <View style={{ opacity: this.opacityAnimatedValue }}>
     {children}
 </View>

用这个:

 <Animated.View style={{ opacity: this.opacityAnimatedValue }}>
     {children}
 </Animated.View>

关于父子组件的 React-Native 并行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505010/

相关文章:

ios - 排毒:iOS模拟器如何确认警报信息

ios - 无法在 CocoaPods 中安装 React 子规范

android - React-native 项目构建失败 : Could not find com. android.tools.lint :lint-gradle:26. 1.0

javascript - 带有两个键的导航在 React Native 中不起作用

javascript - 如何在 React-navigation/react-native 中隐藏后退按钮

reactjs - Flatlist onEndReached 无限循环

react-native - React Native-Expo Audio停止所有声音

javascript - 通过属性高性能缓存搜索大对象

javascript - Picker 示例给出了 TypeError

android - 错误: Requiring module "node_modules\react-native-reanimated\src\Animated.js"