ScrollView组件的基本使用

本文记录了在使用ScrollView时遇到的问题,即在拖动开始(onScrollBeginDrag)暂停图片播放,拖动结束(onScrollEndDrag)后继续播放时出现的异常。寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面代码是在ES6语法,实现图片的播放,但拖到时停止(onScrollBeginDrag)和停止拖动(onScrollEndDrag)后继续播放会出现问题,做个记录,也请大神指点一下。
在这里插入图片描述

import React, {Fragment,Component} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  Image
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import defaults from "@babel/runtime/helpers/esm/defaults";

const Dimensions = require('Dimensions');
const {width} = Dimensions.get('window');

//引入json数据
let ImageData = require('./ImageData.json');


export default class App extends Component{

  static defaultProps = {
    duration:1000
  };

  constructor(props) {
    super(props);
    this.state = {
      currentPage:0
    }
  }

  render(){
    return (
        <Fragment>
          <View style={styles.container}>
            <ScrollView
                ref="scrollView"
                horizontal={true}
                //隐藏水平滚动条
                showsHorizontalScrollIndicator ={false}
                //自动分页
                pagingEnabled={true}
                //当一帧滚动结束
                onMomentumScrollEnd={(e)=>this.onAnmationEnd(e)}
                //开始拖拽
                onScrollBeginDrag={this.onScrollBeginDrag}
                //停止拖拽
                onScrollEndDrag={this.onScrollEndDrag}
            >
              {this.renderAllImage()}
            </ScrollView>
            {/*返回指示器*/}
            <View style={styles.pageViewStyle}>
              {/*返回5个圆点*/}
              {this.renderPageCircle()}
            </View>
          </View>
        </Fragment>
    );
  }

  onScrollBeginDrag(){
    //停止定时器
    //this.timer && clearInterval(this.timer);
  }

  onScrollEndDrag(){
    //开启定时器
    // this.startTimer();
  }
  //实现一些复杂的操作
  componentDidMount() {
    //开启定时器
    this.startTimer();
  }

  componentWillUnmount() {
    this.timer && clearInterval(this.timer);
  }

  //开启定时器
  startTimer(){
    //1.拿到scrollView
    let scrollView = this.refs.scrollView;
    let imgCount = ImageData.data.length;

    //2.添加定时器 this.timer -->可以理解成一个隐式的全局变量
    this.timer = setInterval(()=>{
      //2.1 设置圆点
      let activePage = 0;
      //2.2 判断
      if ((this.state.currentPage + 1)>=imgCount){//越界
        activePage = 0;
      }else {
        activePage = this.state.currentPage + 1;
      }

      //2.3 更新状态机
      this.setState({
        currentPage:activePage
      })

      //2.4 让scrollView滚动起来
      let offsetX = activePage * width;
      scrollView.scrollResponderScrollTo({x:offsetX,y:0,animated:true});

    },this.props.duration);
  }

  //当一帧滚动结束的时候调用
  onAnmationEnd(e){
    //1.求出水平方向的偏移量
    let offSetX = e.nativeEvent.contentOffset.x;

    //2.求出当前的页数
    let currentPage = Math.floor( offSetX / width);
    console.log(currentPage);

    //3.更新状态机,重新绘制UI
    this.setState({
      //当前的页面
      currentPage:currentPage
    })
  }

  //返回所有的图片
  renderAllImage(){
    //数组
    let allImage = [];
    //拿到图像数组
    let imgsArr = ImageData.data;
    //遍历
    for(let i=0;i<imgsArr.length;i++){
      //取出单独的每一个对象
      var imgItem = imgsArr[i];
      //创建组件装入数组
      allImage.push(
          <Image key={i} source={{uri:imgItem.img}} style={{width:width,height:120}}/>
      )
    }
    //返回数组
    return allImage;
  }

  //返回所有的圆点
  renderPageCircle(){
    //定义一个数组放置所有的圆点
    let indicatorArr = [];
    let style;
    //拿到图像数组
    let imgsArr = ImageData.data;
    //遍历
    for (let i=0;i<imgsArr.length;i++){
      //判断
      style = (i==this.state.currentPage) ? {color:'orange'} :{color: '#ffffff'};
      //把圆点装入数组
      indicatorArr.push(
          <Text key={i} style={[{fontSize:25},style]}>&bull;</Text>
      )
    }
    return indicatorArr;
  }
};


const styles = StyleSheet.create({
  container: {
    marginTop: 25
  },
  pageViewStyle:{
    width:width,
    height:25,
    backgroundColor: 'rgba(0,0,0,0.4)',

    //定位
    position:'absolute',
    bottom:0,

    //设置主轴的方向
    flexDirection:'row',
    //设置侧轴方向的对齐方式
    alignItems:'center'
  }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值