SlidngScale.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. }) {
  23. const scrollViewRef = useRef<any>();
  24. const minNum: number = props.min;//props.step < 1 ?props.default_value-2:props.default_value-20//
  25. const maxNum: number = props.max;//props.step < 1 ?props.default_value+2:props.default_value+20//
  26. const min = props.min//(props.default_value - props.step * 20) < props.min ? props.min : props.default_value - props.step * 20
  27. const max = props.max//(props.default_value + props.step * 20) > props.max ? props.max : props.default_value + props.step * 20
  28. // const [minNum, setMinNum] = useState(min)
  29. // const [maxNum, setMaxNum] = useState(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 [list, setList] = useState([])
  34. const slidngWidth = 10
  35. const [current, setCurrent] = useState(props.default_value)
  36. const [left, setLeft] = useState((props.default_value - min) * slidngWidth / props.step);
  37. const [isDraging, setIsDraging] = useState(false)
  38. const [isEnd, setIsEnd] = useState(true)
  39. const [enableText, setEnableText] = useState(true)
  40. for (var i: number = minNum; i <= maxNum; i += props.step) {
  41. var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale));
  42. var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0;
  43. var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  44. list.push({
  45. value: props.step < 1 ? value : Math.round(value),
  46. showBig: isBig && i >= minNum && i <= maxNum,
  47. showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum
  48. })
  49. }
  50. // const [showScrollView, setShowScrollView] = useState(false)
  51. useEffect(() => {
  52. // var array: any = []
  53. // for (var i: number = min; i <= max; i += props.step) {
  54. // var value: number = parseFloat(i.toFixed(props.scale));
  55. // var isBig: boolean = Math.round((value - min) * jingdu) % Math.round(stepNum * jingdu) == 0;
  56. // var isMiddle: boolean = (Math.round((value - min) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  57. // array.push({
  58. // value: props.step < 1 ? value : Math.round(value),
  59. // showBig: isBig && i >= min && i <= max,
  60. // showMiddle: isMiddle && !isBig && i >= min && i <= max
  61. // })
  62. // }
  63. // setList(array)
  64. // setTimeout(() => {
  65. // var min2 = props.min
  66. // var max2 = props.max
  67. // setMinNum(min2)
  68. // setMaxNum(max2)
  69. // setLeft((props.default_value - min2) * slidngWidth / props.step)
  70. // var array2: any = []
  71. // for (var i: number = min2; i <= max2; i += props.step) {
  72. // var value: number = parseFloat(i.toFixed(1));
  73. // var isBig: boolean = Math.round((value - min2) * jingdu) % Math.round(stepNum * jingdu) == 0;
  74. // var isMiddle: boolean = (Math.round((value - min2) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  75. // array2.push({
  76. // value: props.step < 1 ? value : Math.round(value),
  77. // showBig: isBig && i >= min2 && i <= max2,
  78. // showMiddle: isMiddle && !isBig && i >= min2 && i <= max2
  79. // })
  80. // }
  81. // setList(array2)
  82. // }, 50)
  83. }, [])
  84. useEffect(() => {
  85. if (process.env.TARO_ENV == 'weapp') {
  86. if (isEnd && !isDraging) {
  87. props.updateStatus(true)
  88. setEnableText(true)
  89. }
  90. else {
  91. props.updateStatus(false)
  92. setEnableText(false)
  93. }
  94. }
  95. }, [isEnd, isDraging])
  96. function scrollContent(e) {
  97. if (process.env.TARO_ENV == 'rn') {
  98. update(e)
  99. setIsDraging(false)
  100. setIsEnd(true)
  101. return
  102. }
  103. lastValue = e.detail.scrollLeft;
  104. if (timerA)
  105. clearTimeout(timerA);
  106. timerA = setTimeout(() => {
  107. update(e)
  108. setIsDraging(false)
  109. }, 250) as any
  110. }
  111. const handleScroll = throttle((e) => {
  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. var strValue = (minNum + Math.round(count) * props.step).toFixed(props.scale == 0 ? 1 : props.scale);
  141. if ((strValue as any) < minNum) {
  142. strValue = minNum as any;
  143. }
  144. if ((strValue as any) > maxNum) {
  145. strValue = maxNum as any;
  146. }
  147. if (parseFloat(strValue) != current) {
  148. var data = strValue as any;
  149. if (props.step < 1) {
  150. data = parseFloat(strValue).toFixed(props.scale == 0 ? 1 : props.scale);
  151. if (data.indexOf('.') > 0) {
  152. const regexp = /(?:\.0*|(\.\d+?)0+)$/
  153. data = data.replace(regexp, '$1')
  154. }
  155. }
  156. else {
  157. data = Math.round(data);
  158. }
  159. if (process.env.TARO_ENV == 'rn') {
  160. const ReactNativeHapticFeedback = require("react-native-haptic-feedback")
  161. // Optional configuration
  162. const options = {
  163. enableVibrateFallback: true,
  164. ignoreAndroidSystemSettings: false,
  165. };
  166. // Trigger haptic feedback
  167. ReactNativeHapticFeedback.trigger("impactLight", options);
  168. }
  169. setCurrent(data);
  170. props.changed(data);
  171. }
  172. }
  173. const scrollEnd = (e) => {
  174. }
  175. function itemContent(data) {
  176. var item = list[data.index]
  177. var index = data.index
  178. return <View style={{ width: 2, height: 100, backgroundColor: 'red', marginRight: 8 }}></View>
  179. // return <View className={item.showBig ? 'slidng_item_big' : item.showMiddle ? 'slidng_item_middle' : 'slidng_item'} style={{ width: 2, marginRight: 8, backgroundColor: props.themeColor, zIndex: 0 }} key={index}>
  180. // {
  181. // item.showBig ? <Text className="slidng_text">{item.value}</Text> : null
  182. // }
  183. // </View>
  184. }
  185. // var svg2 = `<svg xmlns="http://www.w3.org/2000/svg" width="${list.length * 10 - 8}" height="50">`;
  186. // for (var i = 0; i < list.length; i++) {
  187. // var obj = list[i];
  188. // svg2 += `<line x1="${i * 10}" y1="0" x2="${i * 10}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  189. // if (obj.showBig) {
  190. // svg2 += `<text x="${i * 10}" y="${rpxToPx(30 + 36)}" text-anchor="middle" fill="white" font-size="${rpxToPx(36)}">${obj.value}</text>`
  191. // }
  192. // }
  193. // svg2 += `</svg>`
  194. // console.log(svg2)
  195. // debugger
  196. var svgLine = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="${rpxToPx(28)}">`;
  197. var svgNumber = `<svg xmlns="http://www.w3.org/2000/svg" width="${list.length * 10 - 8 + 60}" height="${rpxToPx(38)}">`;
  198. for (var i = 0; i < 10; i++) {
  199. var obj = list[i];
  200. 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"/>`
  201. }
  202. for (var i = 0; i < list.length; i++) {
  203. var obj = list[i];
  204. if (obj.showBig) {
  205. //字体设置参考 https://segmentfault.com/a/1190000006110417
  206. svgNumber += `<text x="${i * 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)}">${obj.value}</text>`
  207. }
  208. }
  209. svgLine += `</svg>`
  210. svgNumber += `</svg>`
  211. var basestr = encodeURIComponent(svgLine)
  212. var imgLines = `url("data:image/svg+xml,${basestr}");`
  213. var basestr2 = encodeURIComponent(svgNumber)
  214. var imgNum = `url("data:image/svg+xml,${basestr2}");`
  215. var svgLine2 = `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">`
  216. svgLine2 += `<pattern id="pattern" width="100" height="${rpxToPx(28)}" patternUnits="userSpaceOnUse">`
  217. for (var i = 0; i < 10; i++) {
  218. var obj = list[i];
  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. svgLine2 += `</pattern>
  222. <rect width="100%" height="100%" fill="url(#pattern)" />
  223. </svg>`
  224. const isLeonTest = true
  225. return <View className="slidng">
  226. <View className="number_bg" style={{ opacity: enableText ? 1 : 0.4 }}>
  227. {
  228. 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> :
  229. <Text className="number">{current}</Text>
  230. }
  231. <Text className="unit">{props.unit}</Text>
  232. </View>
  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. {
  256. !isLeonTest && list.map((item, index) => {
  257. return <View className={(item as any).showBig ? 'slidng_item_big' : (item as any).showMiddle ? 'slidng_item_middle' : 'slidng_item'}
  258. style={{ width: 2, marginRight: list.length - 1 == index ? 0 : 8, backgroundColor: props.themeColor, zIndex: 0 }} key={index}>
  259. {
  260. (item as any).showBig ? <Text className="slidng_text">{(item as any).value}</Text> : null
  261. }
  262. </View>
  263. })
  264. }
  265. {
  266. isLeonTest && <View>
  267. <View style={{ width: list.length * 10 - 8, height: 100 }}>
  268. {
  269. process.env.TARO_ENV == 'weapp' && <View style={{ width: list.length * 10 - 8, height: rpxToPx(28), backgroundImage: imgLines }} />
  270. }
  271. {
  272. process.env.TARO_ENV == 'rn' && <SvgXml xml={svgLine2} width={list.length * 10 - 8} height={rpxToPx(28)} />
  273. }
  274. {process.env.TARO_ENV == 'weapp' &&
  275. <View style={{ width: list.length * 10 - 8 + 60, marginLeft: -30, height: rpxToPx(38), backgroundImage: imgNum }} />}
  276. {
  277. process.env.TARO_ENV == 'rn' &&
  278. <View style={{ flexDirection: 'row', marginLeft: -30 }}>
  279. <SvgXml xml={svgNumber} width={list.length * 10 - 8 + 60} height={rpxToPx(38)} />
  280. </View>
  281. }
  282. </View>
  283. </View>
  284. }
  285. </View>
  286. <View className="scrollPadding" />
  287. </View>
  288. </ScrollView>
  289. {/* <VirtualList height={rpxToPx(300)} width={rpxToPx(750)} onScroll={handleScroll} layout="horizontal"
  290. initialScrollOffset={left}
  291. itemCount={list.length}
  292. item={itemContent}
  293. itemSize={10}
  294. itemData={list}
  295. /> */}
  296. <Image className="center_line" src={require('@assets/images/scale_center.png')} />
  297. {/* <View className="center_line" /> */}
  298. </View>
  299. </View>
  300. }