在 React Native 中创建一个优化良好的登录屏幕通常涉及处理键盘交互,以避免输入字段被键盘隐藏。在本指南中,我们将逐步构建一个具有动画调整功能的键盘感知登录屏幕,利用自定义挂钩来管理键盘偏移高度。我们还将添加标题图像并组织屏幕以获得美观且实用的布局。
自定义钩子 useKeyboardOffsetHeight 侦听键盘显示/隐藏事件并返回键盘高度,这对于动画布局调整至关重要。此挂钩还确保该功能在 iOS 和 Android 上都能正常工作。
import { useEffect, useState } from 'react'; import { Keyboard } from 'react-native'; export default function useKeyboardOffsetHeight() { const [keyboardOffsetHeight, setKeyboardOffsetHeight] = useState(0); useEffect(() => { const showListener = Keyboard.addListener('keyboardWillShow', (e) => { setKeyboardOffsetHeight(e.endCoordinates.height); }); const hideListener = Keyboard.addListener('keyboardWillHide', () => { setKeyboardOffsetHeight(0); }); const androidShowListener = Keyboard.addListener('keyboardDidShow', (e) => { setKeyboardOffsetHeight(e.endCoordinates.height); }); const androidHideListener = Keyboard.addListener('keyboardDidHide', () => { setKeyboardOffsetHeight(0); }); return () => { showListener.remove(); hideListener.remove(); androidShowListener.remove(); androidHideListener.remove(); }; }, []); return keyboardOffsetHeight; }
主要组件使用自定义 useKeyboardOffsetHeight 挂钩和动画 API 来管理登录表单的平滑过渡。该表单包括电子邮件和密码字段、登录按钮和标题图像。
import React, { useEffect, useRef, useState } from 'react'; import { Animated, Image, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native'; import useKeyboardOffsetHeight from './useKeyboardOffsetHeight'; const App = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const keyboardOffsetHeight = useKeyboardOffsetHeight(); const animatedValue = useRef(new Animated.Value(0)).current; const handleSignIn = () => { // Handle sign-in logic here console.log('Email:', email); console.log('Password:', password); }; // Animate view based on keyboard height useEffect(() => { Animated.timing(animatedValue, { toValue: keyboardOffsetHeight ? -keyboardOffsetHeight * 0.5 : 0, // adjust "0.5" as per requirement to adjust scroll position duration: 500, useNativeDriver: true, }).start(); }, [keyboardOffsetHeight]); return ( <View style={styles.container}> <View style={{ flex: 1 }}> <Image source={{ uri: 'https://cdn.shopaccino.com/igmguru/articles/Become-React-Native-Developer.png?v=496', }} style={styles.image} resizeMode="cover" /> </View> <Animated.ScrollView bounces={false} keyboardShouldPersistTaps="handled" keyboardDismissMode="on-drag" style={{ transform: [{ translateY: animatedValue }] }} contentContainerStyle={styles.box} > <Text style={styles.title}>Sign In</Text> <TextInput style={styles.input} placeholder="Email" value={email} onChangeText={setEmail} keyboardType="email-address" autoCapitalize="none" /> <TextInput style={styles.input} placeholder="Password" value={password} onChangeText={setPassword} secureTextEntry /> </Animated.ScrollView> <TouchableOpacity style={styles.signInButton} onPress={handleSignIn}> <Text style={styles.buttonText}>Sign In</Text> </TouchableOpacity> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, padding: 20, backgroundColor: '#f9f9f9', }, image: { flex: 1, borderRadius: 10, }, box: { flex: 1, width: '100%', backgroundColor: 'lightblue', padding: 20, borderRadius: 10, }, title: { fontSize: 24, fontWeight: 'bold', textAlign: 'center', marginBottom: 20, }, input: { height: 50, borderColor: '#ddd', borderWidth: 1, borderRadius: 8, paddingHorizontal: 10, marginBottom: 15, fontSize: 16, backgroundColor: '#f9f9f9', }, signInButton: { width: '100%', marginTop: 20, backgroundColor: '#4a90e2', borderRadius: 8, paddingVertical: 15, alignItems: 'center', marginBottom: 40, }, buttonText: { color: '#fff', fontSize: 18, fontWeight: 'bold', }, }); export default App;
这个键盘感知登录屏幕通过以下方式提供流畅、用户友好的体验:
通过使用这种方法,您可以创建一个精美且实用的文本输入 UI,特别是在屏幕空间和用户与键盘的交互是关键考虑因素的移动设备上。此设置可以通过更多表单字段或功能进行扩展,为任何 React Native 身份验证流程提供良好的基础。
以上是在 React Native 中构建流畅的、键盘感知的登录屏幕的详细内容。更多信息请关注PHP中文网其他相关文章!