您可以使用 Animated Api 下面是一个 Animated Api 的示例:
import React from "react";
import { Pressable, Animated } from "react-native";
const Component = () => {
const animated = new Animated.Value(1);
const fadeIn = () => {
Animated.timing(animated, {
toValue: 0.4,
duration: 100,
useNativeDriver: true,
}).start();
};
const fadeOut = () => {
Animated.timing(animated, {
toValue: 1,
duration: 200,
useNativeDriver: true,
}).start();
};
return (
<View
style={{ flex: 1, justifyContent: "center", alignItems: "center" }}
>
<Pressable onPressIn={fadeIn} onPressOut={fadeOut}>
<Animated.View
style={{
opacity: animated,
backgroundColor: "red",
padding: 50,
borderRadius: 20,
}}
>
<Text>Text</Text>
</Animated.View>
</Pressable>
</View>
);
};
动画 API 文档:
https ://reactnative.dev/docs/animated
我还建议查看 reanimated 库以创建具有本机性能的动画
https://docs.swmansion.com/react-native-reanimated/