react-native实现轮播

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

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


export default class HelloWorldApp extends Component {

    constructor() {
        super(...arguments);
        this.state = {list: [1, 2, 3], current: 0};
        this.timer=null;
        this.point = 0
    }

    createPointer() {
        return (<View style={S.dot}>{this.state.list.map((item, index) => {
            var color = index === this.state.current ? {color: 'blue'} : {color: 'white'}
            return <Text key={index} style={[{
                fontSize: 25,
                marginLeft: 20
            }, color]}
            >&bull;
            </Text>
        })
        }</View>)
    }
//自动轮播
    move = (e) => {
        this.setState({current: e.nativeEvent.contentOffset.x / width})
    }
//拖拽开始
    drag=()=>{
        this.timer&&clearInterval(this.timer)
    }
//拖拽结束
    end=()=>{
        var scroll=this.refs.scroll;
        this.timer=setInterval(() => {
            this.point++;
            if (this.point > this.state.list.length - 1) {
                this.point = 0
            }
            this.setState({current: this.point})
            scroll.scrollResponderScrollTo({x:this.point*width,y:0,animated:true})
        }, 2000)
    }
    componentDidMount() {
        var scroll=this.refs.scroll;
        this.timer=setInterval(() => {
            this.point++;
           if (this.point > this.state.list.length - 1) {
                this.point = 0
            }
            this.setState({current: this.point})
            scroll.scrollResponderScrollTo({x:this.point*width,y:0,animated:true})
        }, 2000)
    }

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

                <StatusBar hidden={true} translucent={true}/>
                <ScrollView horizontal={true} showsHorizontalScrollIndicator={false}
                            onMomentumScrollEnd={this.move}
                            ref={'scroll'}
                            onScrollBeginDrag={this.drag}
                            onScrollEndDrag={this.end}
                            pagingEnabled={true}>
                    <View>
                        <Image source={require('./src/Image/2.png')}
                               style={{height: 200, width}}
                        ></Image>
                    </View>
                    <View>
                        <Image source={require('./src/Image/2.png')}
                               style={{height: 200, width}}
                        ></Image>
                    </View>
                    <View>
                        <Image source={require('./src/Image/2.png')}
                               style={{height: 200, width}}
                        ></Image>
                    </View>
                </ScrollView>
                <View style={S.num}>
                    {this.createPointer()}
                </View>
            </View>
        );
    }
}

const S = StyleSheet.create({

    container: {
        position: 'relative',
    },
    num: {
        position: 'absolute',
        flexDirection: 'row',
        bottom: 0,
        width,
        height: 30,
        backgroundColor: 'rgba(0,0,0,0.5)',
    },
    dot: {
        flexDirection: 'row',
        width: 150,
        height: 30,
        marginLeft: 180,

        //borderRadius:'50%', backgroundColor: 'white'

    }
})

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值