首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环进度条如何在react本机中以持续时间完成

循环进度条如何在react本机中以持续时间完成
EN

Stack Overflow用户
提问于 2017-05-03 16:16:03
回答 2查看 2.8K关注 0票数 0

我想创建一个循环进度条,它在8秒或更长时间内完成,我使用此代码,但不工作

代码语言:javascript
复制
   import React,{Component} from 'react';
import {
  Image,
  AppRegistry,
  StatusBar,
  StyleSheet,
  TouchableOpacity,
  View,Alert,Animated,Text
} from 'react-native';
import { AnimatedCircularProgress } from 'react-native-circular-progress';
import Camera from 'react-native-camera';
import timer from 'react-native-timer';
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  overlay: {
    position: 'absolute',
    padding: 16,
    right: 0,
    left: 0,
    alignItems: 'center',
  },
  topOverlay: {
    top: 0,
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  bottomOverlay: {
    bottom: 0,
    backgroundColor: 'rgba(0,0,0,0.4)',
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
  captureButton: {
    padding: 10,
    backgroundColor: 'white',
    borderRadius: 20,
  },
  typeButton: {
    padding: 5,
  },
  flashButton: {
    padding: 5,
  },
  buttonsSpace: {
    width: 10,
  },
});
const MAX_POINTS = 500;
export default class Example extends React.Component {
  constructor(props) {
    super(props);

    this.camera = null;

    this.state = {
      camera: {
        aspect: Camera.constants.Aspect.fill,
        captureTarget: Camera.constants.CaptureTarget.cameraRoll,
        type: Camera.constants.Type.back,
        orientation: Camera.constants.Orientation.auto,
        flashMode: Camera.constants.FlashMode.auto,
      },
      isRecording: false,
      timer1:0,
      points: 0,
      fill:0,
    };
  }
  componentDidMount() {
    this.refs.circularProgress.performLinearAnimation(100, 8000);
  }
  takePicture = () => {
    if (this.camera) {
      this.camera.capture()
        .then((data) => console.log(data))
        .catch(err => console.error(err));
    }
  }

  startRecording = () => {     
    if (this.camera) {
      this.camera.capture({mode: Camera.constants.CaptureMode.video})
          .then((data) => console.log(data))
          .catch(err => console.error(err));
      this.setState({
        isRecording: true},()=>timer.setTimeout(this,'dfdf',()=>this.setState({isRecording:false}),8000)
      );
      this.camera.stopCapture();
    }

  }
  stopRecording = () => {
    if (this.camera) {
      this.camera.stopCapture();
      this.setState({
        isRecording: false
      });
    }
  }

  switchType = () => {
    let newType;
    const { back, front } = Camera.constants.Type;

    if (this.state.camera.type === back) {
      newType = front;
    } else if (this.state.camera.type === front) {
      newType = back;
    }

    this.setState({
      camera: {
        ...this.state.camera,
        type: newType,
      },
    });
  }

  get typeIcon() {
    let icon;
    const { back, front } = Camera.constants.Type;

    if (this.state.camera.type === back) {
      icon = require('./ic_camera_rear_white.png');
    } else if (this.state.camera.type === front) {
      icon = require('./ic_camera_front_white.png');
    }

    return icon;
  }

  switchFlash = () => {
    let newFlashMode;
    const { auto, on, off } = Camera.constants.FlashMode;

    if (this.state.camera.flashMode === auto) {
      newFlashMode = on;
    } else if (this.state.camera.flashMode === on) {
      newFlashMode = off;
    } else if (this.state.camera.flashMode === off) {
      newFlashMode = auto;
    }

    this.setState({
      camera: {
        ...this.state.camera,
        flashMode: newFlashMode,
      },
    });
  }

  get flashIcon() {
    let icon;
    const { auto, on, off } = Camera.constants.FlashMode;

    if (this.state.camera.flashMode === auto) {
      icon = require('./ic_flash_auto_white.png');
    } else if (this.state.camera.flashMode === on) {
      icon = require('./ic_flash_on_white.png');
    } else if (this.state.camera.flashMode === off) {
      icon = require('./ic_flash_off_white.png');
    }

    return icon;
  }
    render() {

const fill = this.state.points / MAX_POINTS * 100;
    return (

      <View style={styles.container}>
        <StatusBar
          animated
          hidden
        />
        <Camera playSoundOnCapture={false} captureQuality="720p"
          ref={(cam) => {
            this.camera = cam;
          }}
          style={styles.preview}
          aspect={this.state.camera.aspect}
          captureTarget={this.state.camera.captureTarget}
          type={this.state.camera.type}
          flashMode={this.state.camera.flashMode}
          onFocusChanged={() => {}}
          onZoomChanged={() => {}}
          defaultTouchToFocus
          mirrorImage={false}
        />
        <View style={[styles.overlay, styles.topOverlay]}>
          <TouchableOpacity
            style={styles.typeButton}
            onPress={this.switchType}
          >
            <Image
              source={this.typeIcon}
            />
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.flashButton}
            onPress={this.switchFlash}
          >
            <Image
              source={this.flashIcon}
            />
          </TouchableOpacity>
        </View>
        <View style={[styles.overlay, styles.bottomOverlay]}>
          {
            !this.state.isRecording
            &&
            <TouchableOpacity
                style={styles.captureButton}
                onPress={this.takePicture}
            >
              <Image
                  source={require('./ic_photo_camera_36pt.png')}
              />
            </TouchableOpacity>
            ||
            null
          }
          <View style={styles.buttonsSpace} />
          {
              !this.state.isRecording
              &&
              <TouchableOpacity
                  style={styles.captureButton}
                  onPress={this.startRecording}
              >
                <Image
                    source={require('./ic_videocam_36pt.png')}
                />
              </TouchableOpacity>
              ||
              <TouchableOpacity
                  style={styles.captureButton}
                  onPress={this.stopRecording}
              >
<View style={{alignContent:'center',justifyContent:'center'}}>
                <Image
                    source={require('./ic_stop_36pt.png')}
                >
 <AnimatedCircularProgress
      size={40}
      width={5}
      fill={fill}
      tintColor="#00e0ff"
      backgroundColor="#3d5875"
      ref='circularProgress'
    />
</Image>
</View>


              </TouchableOpacity>
          }
        </View>
      </View>
    );
  }
}
AppRegistry.registerComponent('CameraBuild', () => Example);

我从https://github.com/bgryszko/react-native-circular-progress得到的代码,我在相机上添加相机停止按钮,我调用动画,但这不起作用

EN

回答 2

Stack Overflow用户

发布于 2017-05-03 17:21:08

您不需要重新定义函数performLinearAnimation,并且应该将一些配置传递给组件。像这样的东西应该是有效的:

代码语言:javascript
复制
export default class Example extends React.Component {
componentDidMount(){
    this.refs.circularProgress.performLinearAnimation(100, 8000);
}

render() {
  return (
    <AnimatedCircularProgress
      size={120}
      width={15}
      fill={fill}
      tintColor="#00e0ff"
      backgroundColor="#3d5875"
      ref='circularProgress'
    />
  );
  }
}
AppRegistry.registerComponent('CameraBuild', () => Example);
票数 0
EN

Stack Overflow用户

发布于 2018-06-20 08:38:00

我添加了拉取请求,根据时长获取

https://github.com/bgryszko/react-native-circular-progress/pull/131

只需将performLinearAnimation方法从circularProgress调用到come构造函数中,它就会正常工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43754704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档