基于react native的简单手动切换功能示例

news/2025/4/2 6:23:05/

基于react native的简单手动切换功能示例

    • 代码示例
    • matchTab

代码示例

import React, {useEffect, useRef, useState} from 'react';
import {Animated,PanResponder,StyleSheet,Text,View,Dimensions,ScrollView,
} from 'react-native';
import {pxToPd} from '../../common/js/device';
import MatchTab from './matchTab';const TestExampleTab = ({navigation}) => {const [tabCode, setTabCode] = useState(2);const [tabList, setTabList] = useState([{name: '报名中',code: 2,},{name: '进行中',code: 3,},{name: '已结束',code: 8,},{name: '已作废',code: 11,},]);//手势滑动区域节点const animatedViewRef = useRef(null);//单个切换页面的宽度const deviceWidth = Dimensions.get('window').width;// 默认显示下标的页面let currentIndexRef = useRef(0);//滑动的距离const defaultMove = -currentIndexRef.current * deviceWidth;const pan = useRef(new Animated.Value(defaultMove)).current;const panResponder = useRef(PanResponder.create({onStartShouldSetPanResponder: () => true,//处理手势移动事件,其中使用了`dx`参数来表示在x轴上的移动距离onPanResponderMove: (evt, gestureState) => {//获取当前滚动区域有几个孩子节点const count = animatedViewRef.current._children.length;//每次移动的距离const moveX = -currentIndexRef.current * deviceWidth;//当移动到最左侧或者最右侧时,禁止拖动const start = currentIndexRef.current == 0 && gestureState.dx > 0;const end = currentIndexRef.current == count - 1 && gestureState.dx < 0;if (start || end) {// 禁止继续拖动return false;}if (Math.abs(gestureState.dx) < 50) {return false;}pan.setValue(moveX + gestureState.dx);},//处理手势释放时的逻辑onPanResponderRelease: (_, gestureState) => {//获取当前滚动区域有几个孩子节点const count = animatedViewRef.current._children.length;//当手指拖动区域大于100的时候,开始切换页面if (Math.abs(gestureState.dx) > 100) {let newPageIndex = currentIndexRef.current;if (gestureState.dx > 0) {newPageIndex = Math.max(0, currentIndexRef.current - 1);} else {newPageIndex = Math.min(count - 1, currentIndexRef.current + 1);}const moveX = -newPageIndex * deviceWidth;currentIndexRef.current = newPageIndex;pan.setValue(moveX);} else {pan.setValue(-currentIndexRef.current * deviceWidth);}setTabCode(() => tabList[currentIndexRef.current].code);},}),).current;//tab切换const tabChangeHandle = row => {setTabCode(() => row.status);let subscript = tabList.findIndex(item => item.code == row.status);currentIndexRef.current = subscript;pan.setValue(-subscript * deviceWidth);};useEffect(() => {return () => {};}, []);return (<>{/* tab */}<View style={styles.tabWrap}><MatchTab list={tabList} code={tabCode} onClick={tabChangeHandle} /></View><View style={styles.wraper}><Animated.Viewref={animatedViewRef}style={{width: deviceWidth * tabList.length,flex: 1,flexDirection: 'row',transform: [{translateX: pan}],onStartShouldSetResponderCapture: () => false, // 禁止拦截触摸事件}}{...panResponder.panHandlers}><View style={{width: deviceWidth, backgroundColor: 'red'}}><Text>item One</Text></View><View style={{width: deviceWidth}}><Text>item Two</Text></View><View style={{width: deviceWidth, backgroundColor: 'green'}}><Text>item Three</Text></View><View style={{width: deviceWidth}}><Text>item Four</Text></View></Animated.View></View></>);
};
const styles = StyleSheet.create({// tabtabWrap: {width: '100%',height: pxToPd(80),},wraper: {flex: 1,backgroundColor: '#fff',},
});export default TestExampleTab;

matchTab

import React, {useRef, useState} from 'react';
import {Image,ScrollView,StyleSheet,Text,TouchableOpacity,View,
} from 'react-native';
import {pxToPd} from '../../common/js/device';const MatchTabItem = ({item, current, onClick}) => {const tabItemClickHandle = row => {onClick && onClick(row);};return (<><TouchableOpacitystyle={styles.tabItem}onPress={() => {tabItemClickHandle(item);}}>{item.code == current ? (<><Text style={styles.tabItemNameCurrent}>{item.name}</Text><View style={styles.tabLine}></View></>) : (<><Text style={styles.tabItemName}>{item.name}</Text></>)}</TouchableOpacity></>);
};const MatchTab = ({onClick, list = [], code = 2}) => {//点击tab切换const tabClickHandle = row => {onClick &&onClick({type: 'status',status: row.code,});};return (<><View style={styles.matchTab}><View style={styles.matchTabMenu}><ScrollView horizontal={true}>{list.map(item => (<MatchTabItemkey={item.code}item={item}current={code}onClick={tabClickHandle}/>))}</ScrollView></View></View></>);
};const styles = StyleSheet.create({matchTab: {width: '93.6%',marginLeft: '3.2%',flexDirection: 'row',},matchTabMenu: {flex: 1,},matchTabFilter: {width: pxToPd(120),flexDirection: 'row',justifyContent: 'flex-end',alignItems: 'center',},filterIcon: {width: pxToPd(32),height: pxToPd(32),},filterText: {fontSize: pxToPd(26),fontWeight: '400',color: '#333',marginLeft: pxToPd(10),},filterTextok: {fontSize: pxToPd(26),fontWeight: '400',color: '#FFA157',marginLeft: pxToPd(10),},tabItem: {height: pxToPd(70),position: 'relative',alignItems: 'center',justifyContent: 'center',marginRight: pxToPd(40),},tabItemName: {fontSize: pxToPd(26),fontWeight: '400',color: '#333',},tabItemNameCurrent: {fontSize: pxToPd(30),fontWeight: '500',color: '#ff9f50',},tabLine: {width: pxToPd(32),height: pxToPd(6),backgroundColor: '#ffa157',borderRadius: pxToPd(3),position: 'absolute',left: '50%',marginLeft: -pxToPd(16),bottom: 0,},
});export default MatchTab;

http://www.ppmy.cn/news/1372352.html

相关文章

JavaScript 原型链继承:掌握面向对象的基础

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

1、Ajax、get、post、ajax,随机颜色

一、Ajax初始 1、什么是Ajax&#xff1f; 异步的JavaScript和xml 2、xml是什么&#xff1f; 一种标记语言&#xff0c;传输和存储数据----------现在用JSON传输数据 3、Ajax的作用 局部加载 可以使网页异步更新 4、Ajax的原理或者步骤(6步) 创建Ajax对象 if (window.X…

在Vue中搭建Three.js环境(超详细、保姆级),创建场景、相机、渲染器《一》

目录 Three.js简介创建vue项目引入Three.js实际操作环节文件目录创建初始化场景、相机 Three.js简介 Three.js 是一款基于 WebGL的 JavaScript 3D 库&#xff0c;它封装了 WebGL API&#xff0c;为开发者提供了简单易用的 API 来在 Web 浏览器中展示 3D 图形。Three.js 提供了…

ElasticSearch架构介绍及原理解析

Elasticsearch 是一个高度可扩展的开源全文搜索和分析引擎&#xff0c;用于处理大量的数据。它是由 Elasticsearch BV 公司开发&#xff0c;并且是用 Java 语言编写的。Elasticsearch 基于 Lucene 搜索引擎&#xff0c;提供了 RESTful API&#xff0c;允许你通过 JSON 格式的请…

pdf属性修改教程 如何修改pdf 属性?

PDF&#xff08;Portable Document Format&#xff09;是一种广泛使用的文件格式&#xff0c;用于呈现文档&#xff0c;包括文本、图像、图形和其他元素。PDF文件的一个特点是其属性&#xff0c;这些属性包括文件的创建日期、修改日期、作者、标题、主题、关键词等。这些属性在…

tomcat动静分离和负载均衡

目录 引言 1.实验环境搭建 2.部署Nginx服务器及配置静态页面Web服务 3.部署Tomcat服务及配置动态页面Web服务 4.实验验收 动态页面 静态页面 引言 tomcat服务既可以处理动态页面&#xff0c;也可以处理静态页面&#xff1b;但其处理静态页面的速度远远不如nginx和apache…

材料物理 (HIT) 笔记-2

原内容请参考哈尔滨工业大学何飞教授&#xff1a;https://www.bilibili.com/video/BV18b4y1Y7wd/?p12&spm_id_frompageDriver&vd_source61654d4a6e8d7941436149dd99026962 或《材料物理性能及其在材料研究中的应用》&#xff08;哈尔滨工业大学出版社&#xff09; 三…

mysql SQL优化工作记录(1)

今天生产数据库预警了&#xff0c;马上拉一下慢日志&#xff0c;定位了慢SQL&#xff0c;发出来大家一起学习 select a.fid,a.fname,a.fremark from a where a.fid -1 fid是表的主键&#xff0c;按主键查询怎么会出现扫全表的情况了&#xff0c;原因是fid是字符串类型&#…