当前位置: 首页 > news >正文

ReactNative实现的横向滑动条

OK,我们先看下效果图

注意使用到了两个库

1.react-native-linear-gradient

2.react-native-gesture-handler

ok,我们看下面的代码

import {Image, TouchableWithoutFeedback, StyleSheet, View} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import React from 'react';
import {Gesture,GestureDetector,GestureHandlerRootView,
} from 'react-native-gesture-handler';
export class HorizntalSlider extends React.Component {shouldComponentUpdate(nextProps: Readonly<P>,nextState: Readonly<S>,nextContext: any,): boolean {return false;}constructor(props) {super(props);this.progress = props.initValue;this.step = props.step;this.range = props.max - props.min;this.currentX = 0;this.enable = true;}_setValueChange(value) {this.currentX = value;this.selectedTrack.setNativeProps({style: {width: value},});let indicatorValue = value - 5 > 0 ? value - 5 : 0;this.indicator.setNativeProps({style: {left: indicatorValue - 1},});}componentDidMount(): void {if (this.props) {this.setPowerState(this.props.openState);}}_add() {if (!this.enable) {showToast(this.tips);const {onEnableClick} = this.props;if (onEnableClick) {onEnableClick();}return;}let tempValue = this.progress + this.step;this.progress =tempValue > this.props.max ? this.props.max : tempValue;let styleValue =((this.progress - this.props.min) / this.range) * 250;this._setValueChange(styleValue);const {onLastChange, onChange} = this.props;onChange(this.progress);onLastChange(this.progress);}_reduce() {if (!this.enable) {const {onEnableClick} = this.props;if (onEnableClick) {onEnableClick();}showToast(this.tips);return;}let tempValue = this.progress - this.step;this.progress =tempValue < this.props.min ? this.props.min : tempValue;let styleValue =((this.progress - this.props.min) / this.range) * 250;this._setValueChange(styleValue);const {onLastChange, onChange} = this.props;onChange(this.progress);onLastChange(this.progress);}_onValueChange(x, isFinalize = false) {if (x > 250) {x = 250;}if (x < 0) {x = 0;}this.currentX = x;this.progress = this.props.min + parseInt((x / 250) * this.range);// if (isFinalize) {//   const {onLastChange} = this.props;//   onLastChange(this.progress);// } else {//   const {onChange} = this.props;//   onChange(this.progress);// }this._setValueChange(x);}setPowerState(state) {if (!this.props) {return;}if (state === 1) {this.selectedTrack.setNativeProps({style: {width: this.currentX,},});this.indicator.setNativeProps({style: {opacity: 1},});} else {this.selectedTrack.setNativeProps({style: {width: 0},});this.indicator.setNativeProps({style: {opacity: 0},});}}setEnable(isEnable, tips) {if (!this.props) {return;}this.enable = isEnable;this.tips = tips;}gesture = Gesture.Pan().onBegin(e => {this._onValueChange(e.x);}).onUpdate(e => {this._onValueChange(e.x);}).onFinalize(e => {this._onValueChange(e.x, true);});render() {this.currentX = ((this.progress - this.props.min) / this.range) * 250;this.currentX = this.currentX > 0 ? this.currentX : 0;return (<View style={[styles.mainContainer, this.props.style]}><GestureHandlerRootView><GestureDetector gesture={this.gesture}><View style={styles.sliderContainer}><LinearGradientstart={{x: 0, y: 0}}end={{x: 1, y: 0}}colors={['#4372FF', 'white', '#FF4D4F']}style={{width: 252,height: 60,}}/><Viewstyle={{flexDirection: 'row',alignItems: 'center',position: 'absolute',}}><Viewref={c => (this.selectedTrack = c)}style={{width: this.currentX,opacity: 0,height: 60,}}/><Viewstyle={{flex: 1,backgroundColor: '#12161a',opacity: 0.8,height: 60,}}/></View><Viewref={c => (this.indicator = c)}style={[styles.indicator, {left: this.currentX - 7}]}/></View></GestureDetector></GestureHandlerRootView></View>);}
}class Track extends React.Component {constructor(props) {super(props);this.unitViewArr = [];for (let i = 0; i < 42; i++) {this.unitViewArr[i] = i;}}shouldComponentUpdate(nextProps: Readonly<P>,nextState: Readonly<S>,nextContext: any,): boolean {return false;}render() {return (<View style={styles.trackContainer}>{this.unitViewArr.map((item, index) => {return (<Viewkey={index}style={{flexDirection: 'row', alignItems: 'center'}}><Viewstyle={{height: 60,width: 2,opacity: 0,backgroundColor: '#12161a',borderRadius: 100,}}/><Viewstyle={{height: 60, width: 4, backgroundColor: '#12161a'}}/></View>);})}</View>);}
}const styles = StyleSheet.create({mainContainer: {flexDirection: 'row',justifyContent: 'center',alignItems: 'center',},sliderContainer: {position: 'relative',justifyContent: 'center',paddingVertical: 10,marginLeft: 10,marginRight: 8,},trackContainer: {width: 252,flexDirection: 'row',position: 'absolute',},actionImg: {width: 60,height: 60,},thumb: {height: 34,width: 7,backgroundColor: 'transparent',},indicator: {width: 0,height: 0,position: 'absolute',top: -2,borderLeftWidth: 4,borderTopWidth: 4,borderRightWidth: 4,left: -3,borderTopColor: '#FF6A6B',borderLeftColor: 'transparent',borderRightColor: 'transparent',},
});
export default HorizntalSlider;

使用代码如下

        <GestureHandlerHorizntalSlidermodel={{initValue: 20,step: 10,max: 100,min: 0,}}></GestureHandlerHorizntalSlider>

拖动条:max(最大值),min(最小值),initValue(当前值),step(步调)

相关文章:

ReactNative实现的横向滑动条

OK&#xff0c;我们先看下效果图 注意使用到了两个库 1.react-native-linear-gradient 2.react-native-gesture-handler ok&#xff0c;我们看下面的代码 import {Image, TouchableWithoutFeedback, StyleSheet, View} from react-native; import LinearGradient from reac…...

华为自动驾驶干不过特斯拉?

文 | AUTO芯球 作者 | 李诞 什么&#xff1f; 华为的智能驾驶方案干不过蔚小理&#xff1f; 特斯拉的智能驾驶[FSD]要甩中国车企几条街&#xff1f; 这华为问界阿维塔刚刚推送“全国都能开”的城区“无图 NCA” 就有黑子来喷了 这是跪久了站不起来了吧 作为玩车14年&…...

docker容器stop流程

从API route开始看StopContainer接口的调用过程。 // NewRouter initializes a new container router func NewRouter(b Backend, decoder httputils.ContainerDecoder) router.Router {r : &containerRouter{backend: b,decoder: decoder,}r.initRoutes()return r } ... …...

生产环境_Spark接收传入的sql并替换sql中的表名与解析_非常NB

背景 开发时遇到一个较为复杂的周期需求&#xff0c;为了适配读取各种数据库中的数据并将数据库数据转换为DataFrame并进行后续的开发分析工作&#xff0c;做了如下代码。 在爷们开发这段生产中的代码&#xff0c;可适配mysql,hive,hbase&#xff0c;gbase等等…...

【issue-YOLO】自定义数据集训练YOLO-v7 Segmentation

1. 拉取代码创建环境 执行nvidia-smi验证cuda环境是否可用&#xff1b;拉取官方代码&#xff1b; clone官方代码仓库 git clone https://github.com/WongKinYiu/yolov7&#xff1b;从main分支切换到u7分支 cd yolov7 && git checkout 44f30af0daccb1a3baecc5d80eae229…...

【八大排序】选择排序 | 堆排序 + 图文详解!!

&#x1f4f7; 江池俊&#xff1a; 个人主页 &#x1f525;个人专栏&#xff1a; ✅数据结构冒险记 ✅C语言进阶之路 &#x1f305; 有航道的人&#xff0c;再渺小也不会迷途。 文章目录 一、选择排序1.1 基本思想1.2 算法步骤 动图演示1.3 代码实现1.4 选择排序特性总结 二…...

C语言贪吃蛇详解

个人简介&#xff1a;双非大二学生 个人博客&#xff1a;Monodye 今日鸡汤&#xff1a;人生就像一盒巧克力&#xff0c;你永远不知道下一块是什么味的 C语言基础刷题&#xff1a;牛客网在线编程_语法篇_基础语法 (nowcoder.com) 一.贪吃蛇游戏背景 贪吃蛇是久负盛名的游戏&…...

go使用gopprof分析内存泄露

假设我们使用的是比如beego这样的网络框架,我们可以这样加代码来使用gopprof来进行内存泄露分析: beego框架加gopprof分析代码: 1.先在router.go里添加路由信息: beego.Router("/debug/pprof", &controllers.ProfController{}) beego.Router("/debug…...

uniapp中组件库Mask 遮罩层 的使用方法

目录 #平台差异说明 #基本使用 #嵌入内容 #遮罩样式 #API #Props #Events #Slot 创建一个遮罩层&#xff0c;用于强调特定的页面元素&#xff0c;并阻止用户对遮罩下层的内容进行操作&#xff0c;一般用于弹窗场景 #平台差异说明 AppH5微信小程序支付宝小程序百度小程…...

【数据结构与算法】(7)基础数据结构之双端队列的链表实现、环形数组实现示例讲解

目录 2.6 双端队列1) 概述2) 链表实现3) 数组实现习题E01. 二叉树 Z 字层序遍历-Leetcode 103 2.6 双端队列 1) 概述 双端队列、队列、栈对比 定义特点队列一端删除&#xff08;头&#xff09;另一端添加&#xff08;尾&#xff09;First In First Out栈一端删除和添加&…...

2024 高级前端面试题之 前端工程相关 「精选篇」

该内容主要整理关于 前端工程相关模块的相关面试题&#xff0c;其他内容面试题请移步至 「最新最全的前端面试题集锦」 查看。 前端工程相关模块精选篇 1. webpack的基本配置2. webpack高级配置3. webpack性能优化-构建速度4. webpack性能优化-产出代码&#xff08;线上运行&am…...

CSS常用属性

CSS常用属性 1. 像素的概念 概念&#xff1a;我们的电脑屏幕是&#xff0c;是由一个一个“小点”组成的&#xff0c;每个“小点”&#xff0c;就是一个像素&#xff08;px&#xff09;。规律&#xff1a;像素点越小&#xff0c;呈现的内容就越清晰、越细腻。 注意点&#xff…...

AI新宠Arc浏览器真可以取代Chrome吗?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…...

基于Java (spring-boot)的实验室管理系统

一、项目介绍 普通用户: 1.登录&#xff0c;注册 2.查看实验室列表信息 3.实验室预约 4.查看预约进度并取消 5.查看公告 6.订阅课程 7.实验室报修 8.修改个人信息 教师登录: 1.查看并审核预约申请 2.查看已审核预约并导出到excel 3.实验室设备管理&#xff0c;报修 …...

Android用setRectToRect实现Bitmap基于Matrix矩阵scale缩放RectF动画,Kotlin(一)

Android用setRectToRect实现Bitmap基于Matrix矩阵scale缩放RectF动画&#xff0c;Kotlin&#xff08;一&#xff09; 基于Matrix&#xff0c;控制Bitmap的setRectToRect的目标RectF的宽高。从很小的宽高开始&#xff0c;不断迭代增加setRectToRect的目标RectF的宽高&#xff0c…...

【AI绘画+Midjourney平替】Fooocus:图像生成、修改软件(Controlnet原作者重新设计的UI+Windows一键部署)

代码&#xff1a;https://github.com/lllyasviel/Fooocus windows一键启动包下载&#xff1a;https://github.com/lllyasviel/Fooocus/releases/download/release/Fooocus_win64_2-1-831.7z B站视频教程&#xff1a;AI绘画入门神器&#xff1a;Fooocus | 简化SD流程&#xff0c…...

Java技术栈 —— Hive与HBase

Java技术栈 —— Hive与HBase 一、 什么是Hive与HBase二、如何使用Hive与HBase&#xff1f;2.1 Hive2.1.1 安装2.1.2 使用2.1.2.1 使用前准备2.1.2.2 开始使用hive 2.2 HBase2.2.1 安装2.2.2 使用 三、Apache基金会 一、 什么是Hive与HBase 见参考文章。 一、参考文章或视频链…...

【代码随想录-哈希表】有效的字母异位词

💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学习,不断总结,共同进步,活到老学到老导航 檀越剑指大厂系列:全面总结 jav…...

SQL Server之DML触发器

一、如何创建一个触发器呢 触发器的定义语言如下&#xff1a; CREATE [ OR ALTER ] TRIGGER trigger_nameon {table_name | view_name}{for | After | Instead of }[ insert, update,delete ]assql_statement从这个定义语言我们可以知道如下信息&#xff1a; trigger_name&…...

04. 【Linux教程】安装 Linux 操作系统

通过前面的小节学习&#xff0c;我们已经对 Linux 操作系统有了简单的了解&#xff0c;同时也在 Windows 下安装了虚拟机软件 VMware &#xff0c;那么本节课我们就介绍下如何使用虚拟机软件安装 Linux 操作系统。 通过第一小节的学习我们知道 Linux 有很多的发行版本&#xf…...

Java 8 Stream API 入门到实践详解

一、告别 for 循环&#xff01; 传统痛点&#xff1a; Java 8 之前&#xff0c;集合操作离不开冗长的 for 循环和匿名类。例如&#xff0c;过滤列表中的偶数&#xff1a; List<Integer> list Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens new ArrayList…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

JS设计模式(4):观察者模式

JS设计模式(4):观察者模式 一、引入 在开发中&#xff0c;我们经常会遇到这样的场景&#xff1a;一个对象的状态变化需要自动通知其他对象&#xff0c;比如&#xff1a; 电商平台中&#xff0c;商品库存变化时需要通知所有订阅该商品的用户&#xff1b;新闻网站中&#xff0…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...

通过MicroSip配置自己的freeswitch服务器进行调试记录

之前用docker安装的freeswitch的&#xff0c;启动是正常的&#xff0c; 但用下面的Microsip连接不上 主要原因有可能一下几个 1、通过下面命令可以看 [rootlocalhost default]# docker exec -it freeswitch fs_cli -x "sofia status profile internal"Name …...

Axure 下拉框联动

实现选省、选完省之后选对应省份下的市区...

大数据治理的常见方式

大数据治理的常见方式 大数据治理是确保数据质量、安全性和可用性的系统性方法&#xff0c;以下是几种常见的治理方式&#xff1a; 1. 数据质量管理 核心方法&#xff1a; 数据校验&#xff1a;建立数据校验规则&#xff08;格式、范围、一致性等&#xff09;数据清洗&…...

Python学习(8) ----- Python的类与对象

Python 中的类&#xff08;Class&#xff09;与对象&#xff08;Object&#xff09;是面向对象编程&#xff08;OOP&#xff09;的核心。我们可以通过“类是模板&#xff0c;对象是实例”来理解它们的关系。 &#x1f9f1; 一句话理解&#xff1a; 类就像“图纸”&#xff0c;对…...