SlidngScale_副本.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import { ScrollView, View, Text, Image } from "@tarojs/components";
  2. import VirtualList from '@tarojs/components/virtual-list';
  3. import './SlidngScale.scss'
  4. import { useEffect, useRef, useState } from "react";
  5. import Taro from "@tarojs/taro";
  6. import { rpxToPx } from "@/utils/tools";
  7. import { throttle } from 'lodash';
  8. // import { SvgXml } from "react-native-svg";
  9. var timerA = null
  10. var lastValue = 0
  11. let SvgXml;
  12. if (process.env.TARO_ENV == 'rn') {
  13. SvgXml = require("react-native-svg").SvgXml
  14. }
  15. export default function Component(props: {
  16. step: number, min: number,
  17. max: number, default_value: number,
  18. scale: number,
  19. changed: Function, unit: string
  20. themeColor: string,
  21. updateStatus: Function,
  22. special: any
  23. }) {
  24. const scrollViewRef = useRef<any>();
  25. const isFT_IN = props.special == 'FT_IN'
  26. const minNum: number = props.min;//props.step < 1 ?props.default_value-2:props.default_value-20//
  27. const maxNum: number = props.max;//props.step < 1 ?props.default_value+2:props.default_value+20//
  28. const min = props.min//(props.default_value - props.step * 20) < props.min ? props.min : props.default_value - props.step * 20
  29. const max = props.max//(props.default_value + props.step * 20) > props.max ? props.max : props.default_value + props.step * 20
  30. // const [minNum, setMinNum] = useState(min)
  31. // const [maxNum, setMaxNum] = useState(max)
  32. const stepNum: number = props.step * 10;
  33. const jingdu: number = props.step < 0.1 ? 100 : props.step < 1 ? 10 : 1;
  34. const list: any[] = [];
  35. // const [list, setList] = useState([])
  36. const slidngWidth = 10
  37. const [current, setCurrent] = useState(props.default_value)
  38. const [left, setLeft] = useState((props.default_value - min) * slidngWidth * (isFT_IN ? 1.2 : 1) / props.step - 0.5);
  39. const [isDraging, setIsDraging] = useState(false)
  40. const [isEnd, setIsEnd] = useState(true)
  41. const [enableText, setEnableText] = useState(true)
  42. useEffect(()=>{
  43. },[props.unit])
  44. if (isFT_IN) {
  45. for (var i: number = minNum; i <= maxNum * 1.2; i += props.step) {
  46. var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale));
  47. var isBig: boolean = i % 12 == 0;
  48. var isMiddle: boolean = i % 6 == 0 && !isBig
  49. list.push({
  50. value: props.step < 1 ? value : Math.round(value),
  51. showBig: isBig,
  52. showMiddle: isMiddle
  53. })
  54. }
  55. }
  56. else {
  57. for (var i: number = minNum; i <= maxNum; i += props.step) {
  58. var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale));
  59. var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0;
  60. var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  61. list.push({
  62. value: props.step < 1 ? value : Math.round(value),
  63. showBig: isBig && i >= minNum && i <= maxNum,
  64. showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum
  65. })
  66. }
  67. }
  68. useEffect(() => {
  69. if (process.env.TARO_ENV == 'weapp') {
  70. if (isEnd && !isDraging) {
  71. props.updateStatus(true)
  72. setEnableText(true)
  73. }
  74. else {
  75. props.updateStatus(false)
  76. setEnableText(false)
  77. }
  78. }
  79. }, [isEnd, isDraging])
  80. function scrollContent(e) {
  81. if (process.env.TARO_ENV == 'rn') {
  82. update(e)
  83. setIsDraging(false)
  84. setIsEnd(true)
  85. return
  86. }
  87. lastValue = e.detail.scrollLeft;
  88. if (timerA)
  89. clearTimeout(timerA);
  90. timerA = setTimeout(() => {
  91. update(e)
  92. setIsDraging(false)
  93. }, 250) as any
  94. }
  95. const handleScroll = throttle((e) => {
  96. update(e)
  97. }, 500);
  98. function touchEnd() {
  99. setIsEnd(true)
  100. var temp = lastValue
  101. setTimeout(() => {
  102. if (temp == lastValue) {
  103. setIsDraging(false)
  104. }
  105. }, 250)
  106. }
  107. function touchStart() {
  108. if (process.env.TARO_ENV == 'weapp') {
  109. props.updateStatus(false)
  110. setEnableText(false)
  111. }
  112. }
  113. function dragStart(e) {
  114. lastValue = e.detail.scrollLeft;
  115. setIsEnd(false)
  116. setIsDraging(true)
  117. }
  118. function dragEnd(e) {
  119. setIsEnd(true)
  120. }
  121. function update(e) {
  122. const { scrollLeft } = e.detail;
  123. debugger
  124. var count = scrollLeft / slidngWidth;
  125. if (isFT_IN) {
  126. count = count / 1.2
  127. }
  128. var strValue = (minNum + Math.round(count) * props.step).toFixed(props.scale == 0 ? 1 : props.scale);
  129. if ((strValue as any) < minNum) {
  130. strValue = minNum as any;
  131. }
  132. if ((strValue as any) > maxNum) {
  133. strValue = maxNum as any;
  134. }
  135. if (parseFloat(strValue) != current) {
  136. var data = strValue as any;
  137. if (props.step < 1) {
  138. data = parseFloat(strValue).toFixed(props.scale == 0 ? 1 : props.scale);
  139. if (data.indexOf('.') > 0) {
  140. const regexp = /(?:\.0*|(\.\d+?)0+)$/
  141. data = data.replace(regexp, '$1')
  142. }
  143. }
  144. else {
  145. data = Math.round(data);
  146. }
  147. if (process.env.TARO_ENV == 'rn') {
  148. const ReactNativeHapticFeedback = require("react-native-haptic-feedback")
  149. // Optional configuration
  150. const options = {
  151. enableVibrateFallback: true,
  152. ignoreAndroidSystemSettings: false,
  153. };
  154. // Trigger haptic feedback
  155. ReactNativeHapticFeedback.trigger("impactLight", options);
  156. }
  157. setCurrent(data);
  158. props.changed(data);
  159. }
  160. }
  161. const scrollEnd = (e) => {
  162. }
  163. var svgLine = `<svg xmlns="http://www.w3.org/2000/svg" width="${isFT_IN ? 144 : 100}" height="${rpxToPx(28)}">`;
  164. var svgNumber = `<svg xmlns="http://www.w3.org/2000/svg" width="${list.length * 10 - 8 + 60}" height="${rpxToPx(38)}">`;
  165. if (isFT_IN) {
  166. for (var i = 0; i < 12; i++) {
  167. var obj = list[i];
  168. svgLine += `<line x1="${i * 12 + 1}" y1="0" x2="${i * 12 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  169. }
  170. }
  171. else {
  172. for (var i = 0; i < 10; i++) {
  173. var obj = list[i];
  174. svgLine += `<line x1="${i * 10 + 1}" y1="0" x2="${i * 10 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  175. }
  176. }
  177. for (var i = 0; i < list.length; i++) {
  178. var obj = list[i];
  179. if (obj.showBig) {
  180. //字体设置参考 https://segmentfault.com/a/1190000006110417
  181. svgNumber += `<text x="${i * (isFT_IN ? 12 : 10) + 1 + 30}" y="${rpxToPx(2 + 36)}" text-anchor="middle" fill="white" font-family="PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei" font-size="${rpxToPx(36)}">${isFT_IN ?parseInt('' + obj.value / 12):obj.value}</text>`
  182. }
  183. }
  184. svgLine += `</svg>`
  185. svgNumber += `</svg>`
  186. var basestr = encodeURIComponent(svgLine)
  187. var imgLines = `url("data:image/svg+xml,${basestr}");`
  188. var basestr2 = encodeURIComponent(svgNumber)
  189. var imgNum = `url("data:image/svg+xml,${basestr2}");`
  190. var svgLine2 = `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">`
  191. svgLine2 += `<pattern id="pattern" width="${isFT_IN ? 120 : 100}" height="${rpxToPx(28)}" patternUnits="userSpaceOnUse">`
  192. if (isFT_IN) {
  193. for (var i = 0; i < 12; i++) {
  194. var obj = list[i];
  195. svgLine2 += `<line x1="${i * 12 + 1}" y1="0" x2="${i * 12 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  196. }
  197. }
  198. else {
  199. for (var i = 0; i < 10; i++) {
  200. var obj = list[i];
  201. svgLine2 += `<line x1="${i * 10 + 1}" y1="0" x2="${i * 10 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  202. }
  203. }
  204. svgLine2 += `</pattern>
  205. <rect width="100%" height="100%" fill="url(#pattern)" />
  206. </svg>`
  207. return <View className="slidng">
  208. {
  209. isFT_IN && <View className="number_bg" style={{ opacity: enableText ? 1 : 0.4 }}>
  210. {
  211. parseInt(current / 12 + '') > 0 && (process.env.TARO_ENV == 'weapp' ? <Text className="number" style={{ fontFamily: "PingFang SC,Helvetica Neue, Helvetica, Arial, Hiragino Sans GB, Heiti SC, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;" }}>{parseInt(current / 12 + '')}</Text> :
  212. <Text className="number">{parseInt(current / 12 + '')}</Text>)
  213. }
  214. {parseInt(current / 12 + '') > 0 && <Text className="unit" style={{ marginRight: 2 }}>ft</Text>}
  215. {
  216. current % 12 > 0 && (process.env.TARO_ENV == 'weapp' ? <Text className="number" style={{ fontFamily: "PingFang SC,Helvetica Neue, Helvetica, Arial, Hiragino Sans GB, Heiti SC, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;" }}>{current % 12}</Text> :
  217. <Text className="number">{current % 12}</Text>)
  218. }
  219. {
  220. current % 12 > 0 && <Text className="unit">in</Text>
  221. }
  222. </View>
  223. }
  224. {
  225. !isFT_IN && <View className="number_bg" style={{ opacity: enableText ? 1 : 0.4 }}>
  226. {
  227. process.env.TARO_ENV == 'weapp' ? <Text className="number" style={{ fontFamily: "PingFang SC,Helvetica Neue, Helvetica, Arial, Hiragino Sans GB, Heiti SC, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;" }}>{current}</Text> :
  228. <Text className="number">{current}</Text>
  229. }
  230. <Text className="unit">{props.unit}</Text>
  231. </View>
  232. }
  233. <View className="scroll_bg">
  234. <View className="shadow_top" />
  235. <View className="shadow_left" />
  236. <View className="shadow_right" />
  237. <View className="top_line" style={{ backgroundColor: props.themeColor }} />
  238. <ScrollView
  239. style={{ zIndex: 0, position: 'relative' }} ref={scrollViewRef}
  240. showsHorizontalScrollIndicator={false}
  241. scrollX scrollLeft={left} className="scroll"
  242. enablePassive={true}
  243. fastDeceleration={true}
  244. onScrollEnd={scrollEnd}
  245. onDragEnd={dragEnd}
  246. onTouchStart={touchStart}
  247. onTouchEnd={touchEnd}
  248. onDragStart={dragStart}
  249. onScroll={scrollContent}
  250. enhanced
  251. >
  252. <View className="scrollContent">
  253. <View className="scrollPadding" style={{ width: rpxToPx(372) }} />
  254. <View className="content">
  255. <View>
  256. <View style={{ width: list.length * 10 - 8, height: 100 }}>
  257. {
  258. process.env.TARO_ENV == 'weapp' && <View style={{ width: isFT_IN ? list.length * 10 - 8 : list.length * 10 - 8, height: rpxToPx(28), backgroundImage: imgLines, overflow: 'hidden' }} />
  259. }
  260. {
  261. process.env.TARO_ENV == 'rn' && <SvgXml xml={svgLine2} width={list.length * 10 - 8} height={rpxToPx(28)} />
  262. }
  263. {process.env.TARO_ENV == 'weapp' &&
  264. <View style={{ width: list.length * 10 - 8 + 60, marginLeft: -30, height: rpxToPx(38), backgroundImage: imgNum }} />}
  265. {
  266. process.env.TARO_ENV == 'rn' &&
  267. <View style={{ flexDirection: 'row', marginLeft: -30, height: rpxToPx(40) }}>
  268. <SvgXml xml={svgNumber} width={list.length * 10 - 8 + 60} height={rpxToPx(40)} />
  269. </View>
  270. }
  271. </View>
  272. </View>
  273. </View>
  274. <View className="scrollPadding" />
  275. </View>
  276. </ScrollView>
  277. {/* <VirtualList height={rpxToPx(300)} width={rpxToPx(750)} onScroll={handleScroll} layout="horizontal"
  278. initialScrollOffset={left}
  279. itemCount={list.length}
  280. item={itemContent}
  281. itemSize={10}
  282. itemData={list}
  283. /> */}
  284. <Image className="center_line" src={require('@assets/images/scale_center.png')} />
  285. {/* <View className="center_line" /> */}
  286. </View>
  287. </View>
  288. }