轮播图简单实现
2017-03-29 17:36
106 查看
import React, {Component} from 'react'; import { AppRegistry, StyleSheet, Text, View, ScrollView, Image, AlertIOS, } from 'react-native'; var TimerMixin = require('react-timer-mixin'); var Dimensions = require('Dimensions'); var width = Dimensions.get('window').width; var one = 'https://img.alicdn.com/imgextra/i1/2228361831/TB2M8V4lctnpuFjSZFKXXalFFXa_!!2228361831.jpg'; var two = 'https://img.alicdn.com/bao/uploaded/i4/TB11hG9QXXXXXapaXXXXXXXXXXX_!!0-item_pic.jpg_b.jpg'; var three = 'https://img.alicdn.com/bao/uploaded/i4/TB1JusePXXXXXapaXXXXXXXXXXX_!!0-item_pic.jpg_b.jpg' var fore = 'https://img.alicdn.com/bao/uploaded/i3/TB1j7_3KVXXXXanXXXXXXXXXXXX_!!0-item_pic.jpg_b.jpg'; export default class scrollImage extends Component { state = { currentPage:0, } static defaultProps={ duration:2000 } render() { return ( <View style={styles.styleBackView}> <ScrollView style={styles.styleScrollView} ref="scrollView" horizontal={true} pagingEnabled={true} showsHorizontalScrollIndicator={false} //滚动一页结束之后调用 滚动停止 onMomentumScrollEnd = {(obj)=>this.onAnimationEnd(obj)} onScrollBeginDrag={()=>this.scrollBeginDrag()} onScrollEndDrag={()=>this.scrollEndDrag()} > {this.creatImage()} </ScrollView> <View style={styles.stylePage}> {this.creatPoint()} </View> </View> ) } creatImage() { var array = []; //无限滚动 // array.push( // <Image key={4} source={{uri:fore} } // style={styles.styleImage} // /> // ) array.push( <Image key={0} source={{uri:one} } style={styles.styleImage} /> ) array.push( <Image key={1} source={{uri:two} } style={styles.styleImage} /> ) array.push( <Image key={2} source={{uri:three} } style={styles.styleImage} /> ) array.push( <Image key={3} source={{uri:fore} } style={styles.styleImage} /> ) //无限滚动 // array.push( // <Image key={5} source={{uri:one} } // style={styles.styleImage} // /> // ) return array; } creatPoint(){ var array = []; var style; var fontSize; for(var i=0;i<4;i++){ style = (i == this.state.currentPage) ? {color:'green'} : {color:'#ffffff'}; fontSize = (i == this.state.currentPage) ? {fontSize:35} : {fontSize:25}; array.push( <Text key={i} style={[{marginLeft:5},style,fontSize]}> • </Text> ) } return array; } onAnimationEnd(obj){ var offsetx = obj.nativeEvent.contentOffset.x; var currentPage = Math.floor(offsetx/width); //无限滚动 // if (currentPage == 5){ // obj.scrollTo({x:width,y:0,animated:false}); // currentPage = 0; // }else if (currentPage == 0){ // // let scrollX = 4*width; // // obj.scrollTo({x:scrollX,y:0,animated:false}); // // currentPage = 3; // // }else{ // // currentPage = currentPage - 1; // // } this.setState({ currentPage:currentPage }); } //UI加载完成 componentDidMount() { //无限滚动 // var scrollView = this.refs.scrollView; // scrollView.scrollTo({x:width,y:0,animated:false}); this.startTimer() } scrollBeginDrag(){ clearInterval(this.timer); } scrollEndDrag(){ this.startTimer(); } startTimer(){ //获取到scrollVIew var scrollView = this.refs.scrollView; var obj = this; this.timer = setInterval(function () { var activePage = 0; if(obj.state.currentPage>= 3){ activePage = 0; }else { activePage = obj.state.currentPage + 1; } obj.setState({ currentPage:activePage }) var offsetX = activePage * width; scrollView.scrollTo({x:offsetX,y:0,animated:true}); },this.props.duration); } } const styles = StyleSheet.create({ styleBackView: { backgroundColor: 'red', }, styleScrollView: { marginTop: 50, }, styleImage: { width: width, height: 200, backgroundColor: 'yellow' }, stylePage:{ backgroundColor:'rgba(0,0,0,0.3)', width:width, height:30, position:'absolute', bottom:0, alignItems:'center', flexDirection:'row', justifyContent:'flex-end', paddingRight:30, } });
相关文章推荐
- 简单实现[BCB5]:键盘全局钩子[Hook],监视多进程键盘操作。
- XML 简单接口 (SAX2)用Visual Basic 实现的示例
- 简单实现Crystal Report的动态加载
- 跨域名的Cookie实现---简单另类的实现方法
- 用NUnit2.1简单实现.net的测试驱动开发(TDD)
- 内容分页简单实现代码及祥解(C#)
- Dhtml+Js算法:5个小球运动的简化版,变通实现更简单的飞行的图片
- 打印的简单实现,以及更改打印设置
- Editplus + Zend Encoder实现简单PHP源码加密IDE环境
- 一个简单聊天室的两种实现 (fcntl 和 select)
- http断点续传简单实现(java)
- 自己如何实现简单的http服务器
- 简单的在线升级的实现方法
- 简单分析用SPI实现防火墙
- 初学者看过来:简单谈谈 C/C++ 递归的思想,实现,以及和循环的关系。
- MySql实现简单的注册登录(易语言)
- 一个简单的菜单按钮的实现
- Dhtml:用ondrag事件简单的实现鼠标拖动物件.
- Web报表打印·简单实现·(一)
- 用两种方法快速简单的实现窗口淡入淡出