SlidngScale.tsx 14 KB

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