123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import React, { Component } from "react";
- import {
- Platform,
- StyleSheet,
- Text,
- View,
- Image,
- Button,
- PanResponder,
- ImageBackground,
- findNodeHandle,
- UIManager,
- DeviceEventEmitter
- } from "react-native";
- import BasePage from "../BasePage";
- const instructions = Platform.select({
- ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
- android:
- "Double tap R on your keyboard to reload,\n" +
- "Shake or press menu button for dev menu"
- });
- type Props = {};
- export default class SeekBar extends BasePage {
- constructor(props) {
- super(props);
- this.pressStatus = false;
- this.progress = 0;
- }
- config = {
- changeX: 0,
- changeY: 0,
- xDiff: 0,
- yDiff: 0
- };
- state = {
- max: 1,
- view_width: 0,
- icon_transform: [{ scale: 1 }],
- left: 0,
- top: 0,
- touch_down: false
- };
- render() {
- return (
- <View
- style={{
- height: 80,
- width: "100%",
- alignItems: "center",
- justifyContent: "center"
- }}
- >
- <View
- style={{
- height: 20,
- width: "98%"
- }}
- ref={c => {
- this.progressBar = c;
- }}
- onLayout={() => {
- const handle = findNodeHandle(this.progressBar);
- setTimeout(() => {
- UIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
- // console.warn(x, y, width, height, pageX, pageY);
- this.setState({
- view_width: width
- });
- this.setProgress(this.progress);
- });
- }, 0);
- }}
- >
- <ImageBackground
- style={{
- height: 2,
- width: "100%",
- backgroundColor: "#DBDBDB",
- overflow: "hidden",
- borderRadius: 10,
- top: 10
- }}
- >
- <View
- style={{
- height: 2,
- width: this.state.left + 3,
- backgroundColor: "#FFA91C",
- overflow: "hidden",
- borderRadius: 10
- }}
- />
- </ImageBackground>
- <Image
- {...this._panResponder.panHandlers}
- style={{
- height: 14,
- width: 14,
- borderRadius: 10,
- backgroundColor: "#FFA91C",
- transform: this.state.icon_transform,
- left: this.state.left,
- top: 2
- }}
- />
- </View>
- </View>
- );
- }
- componentWillMount() {
- this._panResponder = PanResponder.create({
- onStartShouldSetPanResponder: (evt, gestureState) => true,
- onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
- onMoveShouldSetPanResponder: (evt, gestureState) => true,
- onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
- onPanResponderGrant: (evt, gestureState) => {
- this.pressStatus = true;
- this.config.changeY = evt.nativeEvent.pageY;
- this.config.changeX = evt.nativeEvent.pageX;
- },
- onPanResponderStart: (evt, gestureState) => {
- this.pressStatus = true;
- console.log("onPanResponderStart");
- this.setState({
- icon_transform: [{ scale: 1.5 }],
- touch_down: true
- });
- },
- onPanResponderMove: (evt, gestureState) => {
- this.config.yDiff = evt.nativeEvent.pageY - this.config.changeY;
- this.config.xDiff = evt.nativeEvent.pageX - this.config.changeX;
- this.state.left = this.state.left + this.config.xDiff;
- this.state.top = this.state.top + this.config.yDiff;
- this.config.changeY = evt.nativeEvent.pageY;
- this.config.changeX = evt.nativeEvent.pageX;
- if (
- this.state.left < 0 ||
- this.state.left > this.state.view_width - 14
- ) {
- if (this.state.left < 0) {
- this.state.left = 0;
- } else if (this.state.left > this.state.view_width - 14) {
- this.state.left = this.state.view_width - 14;
- }
- this.setState({
- left: this.state.left,
- top: this.state.top
- });
- } else {
- this.setState({
- left: this.state.left,
- top: this.state.top
- });
- console.log("onPanResponderMove:" + this.state.left);
- }
- },
- onPanResponderEnd: (evt, gestureState) => {
- this.pressStatus = true;
- console.log("onPanResponderEnd");
- },
- onPanResponderTerminationRequest: (evt, gestureState) => true,
- onPanResponderRelease: (evt, gestureState) => {
- if (this.pressStatus) {
- this.props.onPress && this.props.onPress();
- this.setState({
- icon_transform: [{ scale: 1 }],
- touch_down: false
- });
- }
- this.pressStatus = false;
- var sss = this.state.left / (this.state.view_width - 14);
- // alert(sss + "%");
- this.props.touchUpCallBack(this.state.max * sss);
- },
- onPanResponderTerminate: (evt, gestureState) => {
- // 另一个组件已经成为了新的响应者,所以当前手势将被取消。
- },
- onShouldBlockNativeResponder: (evt, gestureState) => {
- // 返回一个布尔值,决定当前组件是否应该阻止原生组件成为JS响应者
- // 默认返回true。目前暂时只支持android。
- //基于业务交互场景,如果这里使用js事件处理,会导致容器不能左右滑动。所以设置成false.
- return false;
- }
- });
- }
- getTouchDown() {
- return this.state.touch_down;
- }
- setMax(setmax) {
- this.setState({
- max: setmax
- });
- }
- getMax() {
- return this.state.max;
- }
- setProgress(progress) {
- if (this.state.view_width > 0) {
- this.progress = progress;
- if (this.state.touch_down) {
- } else {
- var left_percentage;
- if (this.state.max == 0) {
- left_percentage = progress / 1;
- } else {
- left_percentage = progress / this.state.max;
- }
- this.setState({
- left: (this.state.view_width - 14) * left_percentage,
- icon_transform: [{ scale: 1 }]
- });
- }
- }
- }
- }
- const styles = StyleSheet.create({});
- /**
- <SeekBar
- ref={view => (this.seekbar = view)}
- max={100}
- //必须带此方法,作为滑动之后抬起的回调
- touchUpCallBack={this.touch_up_callback.bind(this)}
- progress={this.state.curprogress}
- />
- state = {
- curprogress: 0,
- max: 100
- };
- touch_up_callback(progress) {
- //抬起之后,获取算出来的progress
- this.setState({
- curprogress: progress
- });
- }
- setProgressIcon() {
- //轮询设置值,超过max则停止
- setTimeout(() => {
- if (!this.seekbar.getTouchDown()) {
- this.setState({
- curprogress: this.state.curprogress + 1
- });
- }
- if (this.state.curprogress >= this.state.max) {
- alert("100了");
- } else {
- this.setProgressIcon();
- }
- this.seekbar.setProgress(this.state.curprogress);
- }, 1000);
- }
- */
|