美文网首页
RN中的轮播图

RN中的轮播图

作者: 基本密码宋 | 来源:发表于2017-11-28 23:24 被阅读88次

遇到了关于this的坑



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

var dimensions=require('Dimensions')
var datas=require('./1.json');
var {width, height}=dimensions.get('window')
var TimerMixin = require('react-timer-mixin');

export default class ScrollViewComment extends Component<{}> {

    //注册计时器
    mixins: [TimerMixin]

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

        this.onScrollBeginDrag = this.onScrollBeginDrag.bind(this);
        this.onScrollEndDrag=this.onScrollEndDrag.bind(this)
    }


    //设置默认值
    static defaultProps={
        period:1000  //定时器的间隔时间
    }


    //当render完成后进行初始化定时器
    componentDidMount() {
        this.startTime();
    }

    //开启定时器
    startTime(){


        this.timers=setInterval(()=>{
            //每隔一秒钟进行一次操作
            var scrollView=this.refs.scrollview;
            var size =datas.length;
            var currentPages=0;
            if (this.state.currentPage + 1>=size) {
                currentPages=0;
            }else {
                currentPages=this.state.currentPage+1;
            }
            this.setState({
                currentPage:currentPages
            })

            ///让图片滚动起来
            var offX=this.state.currentPage*width;
            scrollView.scrollResponderScrollTo({
                x:offX,
                y:0,
                animated:true //是否有动画
            })
        },this.props.period)

    }

    /*开始拖动的时候*/
    onScrollBeginDrag(){
        //停止定时器
         clearInterval(this.timers);
    }


    onScrollEndDrag(){
      //当开始拖拽的时候

        this.startTime()
    }





    render() {
        return (
                <View style={styles.container}>
                     <ScrollView

                         ref="scrollview"

                         horizontal={true}/*是否水平*/

                         alwaysBounceHorizontal={true}/*是否水平有弹滑的效果*/

                         pagingEnabled={true} //是否自动点击分页

                         showsHorizontalScrollIndicator={false}/*是否显示水平滚动条*/



                         onMomentumScrollEnd={(e)=>this.onAnimationEnd(e)}/*当开始滚动的时候*/

                         onScrollBeginDrag ={this.onScrollBeginDrag}/*当开始拖动的时候调用*/

                         onScrollEndDrag  ={this.onScrollEndDrag}/*当停止拖拽的时候*/

                     >
                         {this.getScrollviews()}
                     </ScrollView>
                    {/*设置指示器*/}
                    <View style={styles.pageViewStyle}>
                        {/*返回圆点的指示器*/}
                        {this.getPagePoint()}
                    </View>

                </View>
        );
    }

   getScrollviews(){
        var arrays=[];
        for(var i=0;i<datas.length;i++)
        {
            var data=datas[i];
            arrays.push(
                <View key={i} >
                    <Image style={{width:width,height: height/3*2 }}
                           source={{uri:data.instrument}}/>
                </View>
            )
        }
        return arrays;
    }

    getPagePoint(){
       var arrays=[];
       var colorStyle;
        for(var i=0;i<datas.length;i++)
        {
            colorStyle=(this.state.currentPage==i)?{color:'red', fontSize:50}:{color:'white',fontSize:25};
            var data=datas[i];
            arrays.push(
                <View key={i} style={styles.pointStyle} >
                    <Text style={[colorStyle]}>&bull;</Text>
                </View>
            )
        }
        return arrays;
    }

    /*每一帧结束的时候调用*/
    onAnimationEnd(e){

        var offSetX = e.nativeEvent.contentOffset.x;
        //当前的页数
        var currentPage= Math.round(offSetX/width);



        //更新指示器,绘制ui
        this.setState({
            currentPage:currentPage
        });
     }
}


const styles=StyleSheet.create({
    pageViewStyle:{
        width:width,
        height:25,
        backgroundColor:'rgba(0,0,0,0.3)',
        position:'absolute',
        bottom:0,
        flexDirection:'row',
        alignItems:'center',
        justifyContent:'center'
    },
    pointStyle:{
        marginRight:10
    }
})



相关文章

  • RN中的轮播图

    声明:本人使用的是react-native-swiper 以下都是基于此来描述用手多敲敲加深印象 1.首先引入以...

  • RN中的轮播图

    遇到了关于this的坑

  • 从View的源码分析RN自定义控件显示异常问题

    最近为RN做了一个轮播图的控件,在native代码中运行的很好,但是放入RN之后只能显示最开始的两张图,后面就是空...

  • 轮播图优化-实现子view的复用

    轮播图控件刚毕业的同学们就会写了,这里简单介绍下自己在做RN轮播图时一些心得: 回收与复用弃用的子view,至多只...

  • RN Banner轮播图的实现

    React Native 的ScrollView组件实现轮播图.定时任务实现滚动,TouchableWithout...

  • 使用vue-awesome-swiper方法

    在main.js中引入轮播图插件 在基础组件中建立轮播图组件:

  • ReactNative官方的bug,ViewPagerAndro

    项目中由于需要3D效果的广告轮播图,而RN官方并没有提供,于是只能用安卓原生封装了一个3D的轮播图,实际上就是一个...

  • RN原生模块的调用进阶(六)

    假设现在需要在RN页面实现轮播图,当然你可以使用react-native-swiper轻松愉快的搞定,no pro...

  • 2018-03-09

    UICollectionView在一个页面中,既有轮播图 又有UICollectionView时,需要点击轮播图 ...

  • 无标题文章

    轮播图分为:传统轮播图、间歇轮播图、呼吸轮播图、无缝滚动轮播图等。它们各具特色,各有用处。 1.传统轮播图 第一步...

网友评论

      本文标题:RN中的轮播图

      本文链接:https://www.haomeiwen.com/subject/kirybxtx.html