SlidngScale.tsx 13 KB

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