SlidngScale.tsx 15 KB

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