在 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中文網其他相關文章!