您好,登錄后才能下訂單哦!
在React Native中,可以使用第三方庫react-native-gesture-handler
和react-native-reanimated
來實現側滑菜單。下面是一個簡單的側滑菜單實現示例:
npm install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npx react-native init SideMenuExample
cd SideMenuExample
在項目中創建一個名為SideMenu
的新文件夾,并在其中創建兩個文件SideMenu.js
和SideMenu.js
。
在SideMenu.js
文件中,編寫以下代碼:
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import { PanResponder } from 'react-native-gesture-handler';
const SideMenu = ({ navigation }) => {
const [panResponder, setPanResponder] = useState(null);
const [menuVisible, setMenuVisible] = useState(false);
const toggleMenu = () => {
setMenuVisible(!menuVisible);
};
const panResponderHandler = PanResponder.create({
onStartShouldSetResponder: () => true,
onMoveShouldSetResponder: () => true,
onPanResponderGrant: () => {
setPanResponder(panResponderHandler);
},
onPanResponderRelease: () => {
setPanResponder(null);
if (menuVisible) {
setTimeout(toggleMenu, 300);
}
},
});
return (
<View style={styles.container}>
<TouchableOpacity onPress={toggleMenu} style={styles.menuButton}>
<Text style={styles.menuButtonText}>Menu</Text>
</TouchableOpacity>
{menuVisible && (
<View style={[styles.menu, panResponder && styles.menuPanResponder]}>
<View style={styles.menuContent}>
<TouchableOpacity
onPress={() => {
setMenuVisible(false);
navigation.goBack();
}}
style={styles.menuOption}>
<Text style={styles.menuOptionText}>Home</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
setMenuVisible(false);
navigation.navigate('Settings');
}}
style={styles.menuOption}>
<Text style={styles.menuOptionText}>Settings</Text>
</TouchableOpacity>
</View>
</View>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
menuButton: {
backgroundColor: '#007AFF',
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
},
menuButtonText: {
color: '#fff',
fontSize: 16,
},
menu: {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
backgroundColor: '#000',
justifyContent: 'center',
alignItems: 'center',
},
menuContent: {
backgroundColor: '#fff',
paddingHorizontal: 20,
paddingVertical: 20,
borderRadius: 10,
},
menuOption: {
paddingHorizontal: 20,
paddingVertical: 10,
borderBottomWidth: 1,
borderBottomColor: '#ccc',
},
menuOptionText: {
fontSize: 16,
},
menuPanResponder: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
});
export default SideMenu;
App.js
文件中引入并使用SideMenu
組件:import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import SideMenu from './SideMenu';
const Stack = createStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>
<SideMenu navigation={navigation} />
</NavigationContainer>
);
};
export default App;
現在,您應該可以在模擬器或實際設備上運行應用程序,并看到一個可以滑動打開的側滑菜單。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。