import { ScrollView, View, Text, Image } from "@tarojs/components"; import VirtualList from '@tarojs/components/virtual-list'; import './SlidngScale.scss' import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import { rpxToPx } from "@/utils/tools"; import { throttle } from 'lodash'; // import { SvgXml } from "react-native-svg"; var timerA = null var lastValue = 0 let SvgXml; if (process.env.TARO_ENV == 'rn') { SvgXml = require("react-native-svg").SvgXml } export default function Component(props: { step: number, min: number, max: number, default_value: number, scale: number, changed: Function, unit: string themeColor: string, updateStatus: Function }) { const scrollViewRef = useRef(); const minNum: number = props.min;//props.step < 1 ?props.default_value-2:props.default_value-20// const maxNum: number = props.max;//props.step < 1 ?props.default_value+2:props.default_value+20// const min = props.min//(props.default_value - props.step * 20) < props.min ? props.min : props.default_value - props.step * 20 const max = props.max//(props.default_value + props.step * 20) > props.max ? props.max : props.default_value + props.step * 20 // const [minNum, setMinNum] = useState(min) // const [maxNum, setMaxNum] = useState(max) const stepNum: number = props.step * 10; const jingdu: number = props.step < 0.1 ? 100 : props.step < 1 ? 10 : 1; const list: any[] = []; // const [list, setList] = useState([]) const slidngWidth = 10 const [current, setCurrent] = useState(props.default_value) const [left, setLeft] = useState((props.default_value - min) * slidngWidth / props.step); const [isDraging, setIsDraging] = useState(false) const [isEnd, setIsEnd] = useState(true) const [enableText, setEnableText] = useState(true) for (var i: number = minNum; i <= maxNum; i += props.step) { var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale)); var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0; var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0; list.push({ value: props.step < 1 ? value : Math.round(value), showBig: isBig && i >= minNum && i <= maxNum, showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum }) } // const [showScrollView, setShowScrollView] = useState(false) useEffect(() => { // var array: any = [] // for (var i: number = min; i <= max; i += props.step) { // var value: number = parseFloat(i.toFixed(props.scale)); // var isBig: boolean = Math.round((value - min) * jingdu) % Math.round(stepNum * jingdu) == 0; // var isMiddle: boolean = (Math.round((value - min) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0; // array.push({ // value: props.step < 1 ? value : Math.round(value), // showBig: isBig && i >= min && i <= max, // showMiddle: isMiddle && !isBig && i >= min && i <= max // }) // } // setList(array) // setTimeout(() => { // var min2 = props.min // var max2 = props.max // setMinNum(min2) // setMaxNum(max2) // setLeft((props.default_value - min2) * slidngWidth / props.step) // var array2: any = [] // for (var i: number = min2; i <= max2; i += props.step) { // var value: number = parseFloat(i.toFixed(1)); // var isBig: boolean = Math.round((value - min2) * jingdu) % Math.round(stepNum * jingdu) == 0; // var isMiddle: boolean = (Math.round((value - min2) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0; // array2.push({ // value: props.step < 1 ? value : Math.round(value), // showBig: isBig && i >= min2 && i <= max2, // showMiddle: isMiddle && !isBig && i >= min2 && i <= max2 // }) // } // setList(array2) // }, 50) }, []) useEffect(() => { if (process.env.TARO_ENV == 'weapp') { if (isEnd && !isDraging) { props.updateStatus(true) setEnableText(true) console.log('true') } else { props.updateStatus(false) setEnableText(false) console.log('false') } } }, [isEnd, isDraging]) function scrollContent(e) { if (process.env.TARO_ENV == 'rn') { update(e) setIsDraging(false) setIsEnd(true) return } lastValue = e.detail.scrollLeft; if (timerA) clearTimeout(timerA); timerA = setTimeout(() => { update(e) setIsDraging(false) }, 250) as any } const handleScroll = throttle((e) => { console.log(e) update(e) }, 500); function touchEnd() { setIsEnd(true) var temp = lastValue setTimeout(() => { if (temp == lastValue) { setIsDraging(false) } }, 250) } function touchStart() { if (process.env.TARO_ENV == 'weapp') { props.updateStatus(false) setEnableText(false) } } function dragStart(e) { lastValue = e.detail.scrollLeft; setIsEnd(false) setIsDraging(true) } function dragEnd(e) { setIsEnd(true) console.log('end') // setIsDraging(false) } function update(e) { const { scrollLeft } = e.detail; var count = scrollLeft / slidngWidth; var strValue = (minNum + Math.round(count) * props.step).toFixed(props.scale == 0 ? 1 : props.scale); if ((strValue as any) < minNum) { strValue = minNum as any; } if ((strValue as any) > maxNum) { strValue = maxNum as any; } if (parseFloat(strValue) != current) { var data = strValue as any; if (props.step < 1) { data = parseFloat(strValue).toFixed(props.scale == 0 ? 1 : props.scale); if (data.indexOf('.') > 0) { const regexp = /(?:\.0*|(\.\d+?)0+)$/ data = data.replace(regexp, '$1') } } else { data = Math.round(data); } setCurrent(data); props.changed(data); } } const scrollEnd = (e) => { console.log(e, 'end') // const { scrollLeft } = e.detail; // setLeft(scrollLeft) } function itemContent(data) { var item = list[data.index] var index = data.index return // return // { // item.showBig ? {item.value} : null // } // } // var svg2 = ``; // for (var i = 0; i < list.length; i++) { // var obj = list[i]; // svg2 += `` // if (obj.showBig) { // svg2 += `${obj.value}` // } // } // svg2 += `` // console.log(svg2) // debugger var svgLine = ``; var svgNumber = ``; for (var i = 0; i < 10; i++) { var obj = list[i]; svgLine += `` } for (var i = 0; i < list.length; i++) { var obj = list[i]; if (obj.showBig) { //字体设置参考 https://segmentfault.com/a/1190000006110417 svgNumber += `${obj.value}` } } svgLine += `` svgNumber += `` var basestr = encodeURIComponent(svgLine) var imgLines = `url("data:image/svg+xml,${basestr}");` var basestr2 = encodeURIComponent(svgNumber) var imgNum = `url("data:image/svg+xml,${basestr2}");` var svgLine2 = `` svgLine2 += `` for (var i = 0; i < 10; i++) { var obj = list[i]; svgLine2 += `` } svgLine2 += ` ` const isLeonTest = true return { process.env.TARO_ENV == 'weapp' ? {current} : {current} } {props.unit} { !isLeonTest && list.map((item, index) => { return { (item as any).showBig ? {(item as any).value} : null } }) } { isLeonTest && { process.env.TARO_ENV == 'weapp' && } { process.env.TARO_ENV == 'rn' && } {process.env.TARO_ENV == 'weapp' && } { process.env.TARO_ENV == 'rn' && } } {/* */} {/* */} }