This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo GlassEffect

使用 iOS 原生 UIVisualEffectView 渲染液态玻璃效果的 React 组件。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.3

使用 UIVisualEffectView 渲染原生 iOS 液态玻璃效果的 React 组件。支持可自定义的玻璃样式和色调颜色。

🌐 React components that render native iOS liquid glass effect using UIVisualEffectView. Supports customizable glass styles and tint color.

安装

🌐 Installation

Terminal
- npx expo install expo-glass-effect
- yarn expo install expo-glass-effect
- pnpm expo install expo-glass-effect
- bun expo install expo-glass-effect

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

GlassView

GlassView 组件渲染原生 iOS 玻璃效果。它支持不同的玻璃效果样式,并且可以通过着色颜色进行自定义,以满足各种美学需求。

🌐 The GlassView component renders the native iOS glass effect. It supports different glass effect styles and can be customized with tint colors for various aesthetic needs.

Basic GlassView usage
import { StyleSheet, View, Image } from 'react-native'; import { GlassView } from 'expo-glass-effect'; export default function App() { return ( <View style={styles.container}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> {/* Basic Glass View */} <GlassView style={styles.glassView} /> {/* Glass View with clear style */} <GlassView style={styles.tintedGlassView} glassEffectStyle="clear" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, backgroundImage: { ...StyleSheet.absoluteFill, width: '100%', height: '100%', }, glassView: { position: 'absolute', top: 100, left: 50, width: 200, height: 100, borderRadius: 12, justifyContent: 'center', alignItems: 'center', padding: 20, }, tintedGlassView: { position: 'absolute', top: 250, left: 50, width: 200, height: 100, borderRadius: 12, justifyContent: 'center', alignItems: 'center', padding: 20, }, text: { fontSize: 16, fontWeight: '600', color: '#000', textAlign: 'center', }, });

GlassContainer

GlassContainer 组件允许你将多个玻璃视图组合成一个整体效果。

🌐 The GlassContainer component allows you to combine multiple glass views into a combined effect.

GlassContainer example
import { StyleSheet, View, Image } from 'react-native'; import { GlassView, GlassContainer } from 'expo-glass-effect'; export default function GlassContainerDemo() { return ( <View style={styles.container}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1547036967-23d11aacaee0?w=400&h=600&fit=crop', }} /> <GlassContainer spacing={10} style={styles.containerStyle}> <GlassView style={styles.glass1} isInteractive /> <GlassView style={styles.glass2} /> <GlassView style={styles.glass3} /> </GlassContainer> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, backgroundImage: { ...StyleSheet.absoluteFill, width: '100%', height: '100%', }, containerStyle: { position: 'absolute', top: 200, left: 50, width: 250, height: 100, flexDirection: 'row', alignItems: 'center', gap: 5, }, glass1: { width: 60, height: 60, borderRadius: 30, }, glass2: { width: 50, height: 50, borderRadius: 25, }, glass3: { width: 40, height: 40, borderRadius: 20, }, });

动画玻璃效果风格

🌐 Animated glass effect style

glassEffectStyle 属性接受一个包含 animate 和 animationDuration 属性的配置对象,用于在玻璃效果样式之间进行原生动画过渡。这是淡入/淡出玻璃效果的推荐方法,而无需修改 opacity。

🌐 The glassEffectStyle prop accepts a config object with animate and animationDuration properties to natively animate transitions between glass styles. This is the recommended way to fade in/out the glass effect without modifying opacity.

Animated glass effect style
import { useState } from 'react'; import { StyleSheet, Text, View, Image, Pressable } from 'react-native'; import { GlassView } from 'expo-glass-effect'; export default function AnimatedGlassStyleExample() { const [visible, setVisible] = useState(true); return ( <View style={styles.container}> <View style={styles.backgroundImage}> <Image style={{ width: 300, height: 200, }} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> <GlassView style={styles.glassView} glassEffectStyle={{ style: visible ? 'clear' : 'none', animate: true, animationDuration: 0.5, }} /> </View> <Pressable style={styles.toggleButton} onPress={() => setVisible(prev => !prev)}> <Text style={styles.toggleButtonText}>{visible ? 'Hide' : 'Show'} Glass Effect</Text> </Pressable> </View> ); } const styles = StyleSheet.create({ container: { height: 300, width: 300, }, backgroundImage: { position: 'absolute', }, glassView: { position: 'absolute', width: 200, height: 120, borderRadius: 12, }, toggleButton: { position: 'absolute', bottom: 100, alignSelf: 'center', backgroundColor: '#007AFF', paddingVertical: 14, paddingHorizontal: 24, borderRadius: 12, }, toggleButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });

不透明度动画解决方法

🌐 Opacity animation workaround

苹果不建议在 GlassView 或其父视图上将 opacity 值设低于 1,详情请参见 设置正确的 alpha 值。在非常低的透明度下,玻璃效果不会渲染。如果仍想动画化透明度,可以使用 Reanimated 动画一个封装视图的透明度,同时在所需样式和 'none' 之间切换 glassEffectStyle。

🌐 Apple does not recommend opacity values below 1 on GlassView or its parent views, as described in Set the correct alpha value. At very low opacity, the glass effect does not render. To animate opacity anyway, use Reanimated to animate a wrapper view's opacity while toggling the glassEffectStyle between the desired style and 'none'.

Glass effect opacity animation workaround
import { GlassView } from 'expo-glass-effect'; import { StyleSheet, Text, View, Image, Pressable } from 'react-native'; import Animated, { useAnimatedProps, useAnimatedStyle, useSharedValue, withTiming, } from 'react-native-reanimated'; const AnimatedGlassView = Animated.createAnimatedComponent(GlassView); export default function GlassOpacityAnimationExample() { const fadeOpacity = useSharedValue(0); const glassViewProps = useAnimatedProps(() => { const glassEffectStyle = fadeOpacity.value > 0.02 ? 'regular' : 'none'; return { glassEffectStyle, style: { width: 150, height: 100, borderRadius: 12, position: 'absolute', }, }; }); const fadeOpacityStyle = useAnimatedStyle(() => ({ position: 'absolute', opacity: fadeOpacity.value, width: 150, height: 100, borderRadius: 12, })); return ( <> <Text style={styles.title}>Opacity Animation Workaround (iOS 26.1+)</Text> <View style={styles.backgroundContainer}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> <Animated.View style={fadeOpacityStyle}> <AnimatedGlassView animatedProps={glassViewProps} /> </Animated.View> </View> <Pressable style={styles.toggleButton} onPress={() => { fadeOpacity.value = withTiming(fadeOpacity.value > 0.5 ? 0 : 1, { duration: 500 }); }}> <Text style={styles.toggleButtonText}>Toggle glass visibility</Text> </Pressable> </> ); } const styles = StyleSheet.create({ title: { fontSize: 20, fontWeight: 'bold', }, backgroundContainer: { height: 300, borderRadius: 12, overflow: 'hidden', position: 'relative', }, backgroundImage: { width: '100%', height: '100%', }, toggleButton: { backgroundColor: '#007AFF', paddingVertical: 14, paddingHorizontal: 24, borderRadius: 12, alignItems: 'center', }, toggleButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });

isLiquidGlassAvailable

isLiquidGlassAvailable 函数允许你检查编译后的应用中是否可用 Liquid Glass 效果。它会验证系统和编译器版本,以及 Info.plist 设置。

🌐 The isLiquidGlassAvailable function let's you check, if the Liquid Glass effect is available in the compiled application. It validates the system and compiler versions, as well as the Info.plist settings.

import { Text } from 'react-native'; import { isLiquidGlassAvailable } from 'expo-glass-effect'; export default function CheckLiquidGlass() { return ( <Text> {isLiquidGlassAvailable() ? 'Liquid Glass effect is available' : 'Liquid Glass effect is not available'} </Text> ); }

isGlassEffectAPIAvailable

isGlassEffectAPIAvailable 函数用于检查设备在运行时是否可以使用 Liquid Glass API。

🌐 The isGlassEffectAPIAvailable function checks whether the Liquid Glass API is available at runtime on the device.

import { Text } from 'react-native'; import { isGlassEffectAPIAvailable } from 'expo-glass-effect'; export default function CheckGlassEffectAPI() { return ( <Text> {isGlassEffectAPIAvailable() ? 'Glass Effect API is available' : 'Glass Effect API is not available'} </Text> ); }

应用接口

🌐 API

import { GlassView, GlassContainer, isLiquidGlassAvailable, isGlassEffectAPIAvailable, } from 'expo-glass-effect';

No API data file found, sorry!