React Native 之ScrollView
import React, { Component } from 'react'
import { Text, StyleSheet, View, Button ,TouchableOpacity,Alert,ScrollView,Image} from 'react-native'
import { createStackNavigator, createAppContainer } from 'react-navigation'; let Dimensions = require('Dimensions');
let {width, height} = Dimensions.get('window'); export default class HomeScreen extends Component {
static navigationOptions = {
title: 'HomeScreen'
} //defaultProps
static defaultProps = {
//每隔多少秒执行一次
duration:2000
} componentDidMount(){
// 开启定时器
this.startTime();
} // 开启定时器
startTime(){
// 1.拿到scrollerView
let scrollerView = this.refs.scrollerView;
let imageCount = 4;
// 2.添加定时器
// 2.1 设置圆点
let activePage = 0;
this.timer = setInterval(() => {
// 2.2 判断
if((this.state.currentPage+1) >= imageCount){
activePage = 0;
}else {
activePage = this.state.currentPage+1;
}
// 2.3 更新状态机
this.setState({
// 当前页
currentPage: activePage
})
// 2.4 让scrollerVeiw滚动起来
let offsetX = activePage * width;
scrollerView.scrollTo({x: offsetX, y:0, animated:true});
}, this.props.duration);
} constructor(props){
super(props);
this.state = {
currentPage:0
}
this.renderPageIndex = this.renderPageIndex.bind(this);
} renderChildView(){
var allChild = [];
var imgNames = [require('./img/leading_01.png'),
require('./img/leading_02.png'),
require('./img/leading_03.png'),
require('./img/leading_04.png')];
imgNames.forEach((item,index)=>{ allChild.push(
<Image
style={{width: 375,height: 375}}
source={item}
/>
)
})
return allChild;
} // 返回页面指示器的圆点
renderPageIndex(){
// 数组
let indicatorArr = [];
//拿到图形数组
//let imageArrs = ImageData.data;
//样式
var style;
//遍历
for (var i = 0; i < 4; i++){
// 判断
style = (i==this.state.currentPage) ? {color: 'orange'} : {color: '#E8E8E8'} //放入圆点
indicatorArr.push(
// 多个样式使用[]数组来放
<Text key={i} style={[{fontSize:25}, style]}>•</Text>
);
}
//返回
return indicatorArr;
} // 当一帧滚动结束的时候调用
onAnimationEnd(e){
// 1.求出水平方向的偏移量
var offsetX = e.nativeEvent.contentOffset.x; // 2.求出当前的页数 floor函数 取整
var currentPage = Math.floor(offsetX / width); // 3.更新状态机
this.setState({
// 当前页
currentPage: currentPage
}) } // 开始拖拽时调用
onScrollerBeginDrag(){
// 停止定时器
clearInterval(this.timer);
}
// 停止拖拽时调用
onScrollEndDrag(){
// 开启定时器
this.startTime();
} render(){
return(
<View style={styles.circulateViewStyle}>
<ScrollView
ref="scrollerView"
horizontal={true}
pagingEnabled={true}
scrollEnabled={true}
//滚动动画结束时调用此函数
onMomentumScrollEnd={(e)=>this.onAnimationEnd(e)}
//开始拖拽
onScrollBeginDrag={(e)=>this.onScrollerBeginDrag(e)}
//停止拖拽
onScrollEndDrag={(e)=>this.onScrollEndDrag(e)}
> {this.renderChildView()}
</ScrollView>
{/*底部页面指示器*/}
<View style={styles.pageViewStyle}>
{/*返回5个圆点*/}
{this.renderPageIndex()}
</View> </View> );
} const styles = StyleSheet.create({ circulateViewStyle: { marginTop:20,
height:150,
width:width,
},
scrollViewStyle:{ }, imageStyle: {
width: width,
height: 150
},
pageViewStyle: {
width:width,
height:25,
backgroundColor:'rgba(0, 0, 0, 0.4)',
position:'absolute',
bottom:0,
flexDirection:'row',
alignItems:'center'
}
});
React Native 之ScrollView的更多相关文章
- React Native之 ScrollView介绍和使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native之ScrollView控件详解
概述 ScrollView在Android和ios原生开发中都比较常见,是一个 滚动视图控件.在RN开发中,系统也给我们提供了这么一个控件.不过在RN开发中 ,使用ScrollView必须有一个确定的 ...
- react native ScrollView
ScrollView是一个通用的可滚动的容器,你可以在其中放入多个组件和视图,而且这些组件并不需要是同类型的.ScrollView不仅可以垂直滚动,还能水平滚动(通过horizontal属性来设置). ...
- React Native组件之ScrollView 和 StatusBar和TabBarIos
React Native中的组件ScrollView类似于iOS中的UIScrollView,其基本的使用方法和熟悉如下: /** * Sample React Native App * https: ...
- [React Native] Create a component using ScrollView
To show a list of unchanging data in React Native you can use the scroll view component. In this les ...
- React Native常用组件之ScrollView
1. 两个要点 1.1 ScrollView必须有一个确定的高度才能正常工作 它实际上所做的就是将一系列不确定高度的子组件装进一个确定高度的容器(通过滚动操作) 通常有两种做法: 第一种: 直接给该S ...
- React Native常用组件之ScrollView组件
一.前言 从iOS开发的经验来看,scrollView无疑是移动开发中很重要的一个组件,比如后面会学到的ListView就是继承自它.那么,在开发中比如:焦点图.引导页等地方都有其的影子,那接下来我们 ...
- [RN] React Native ScrollView去掉自带的间隔
React Native ScrollView去掉自带的间隔 使用ScrollView时,自带了一个类似marginTop的效果,将其去掉 <ScrollView automaticallyAd ...
- [RN] React Native 中使用 stickyHeaderIndices 实现 ScrollView 的吸顶效果
React Native中,ScrollView组件可以使用 stickyHeaderIndices 轻松实现 sticky 效果. 例如下面代码中: <ScrollView showsVert ...
随机推荐
- 函数参数中经常见到的*args和**kwargs
在python中,这两个是python中的可变参数,*arg表示任意多个无名参数,类型为tuple;**kwargs表示关键字参数,为dict. 例如下面这个代码可以利用*args计算传入的n个数的平 ...
- eclipse sts 常规操作
项目:右键 refresh 右键 maven -> update project 重新remove add project 重启软件,电脑 1.项目冗余 Package Explorer ...
- 第十七周周总结 Swing
考试系统 1.登录功能 用户和密码存在在哪里? 文件 2.考试功能 考试题目和答案存在哪? 文件 3.展示功能 GUI Graphical User Interface图形用户接口 #GUI Java ...
- mysqlreport 安装&使用
安装包:mysqlreport-3.5.tgz 下载地址:http://hackmysql.com/scripts/mysqlreport-3.5.tgz 安装办法:[root@nagios ~]# ...
- ant buid.xml 模板
<?xml version="1.0" encoding="UTF-8"?> <project name="ant" de ...
- Two modules in a project cannot share the same content root报错解决方案
观察上方是否出现两个同样的项目,删除不需要的那个,我觉得是因为两个项目同时引用一个根目录文件导致的.
- Shell的常用十八条命令
Shell的18条常用命令整理 1. ls: 类似于dos下的dir命令 ls最常用的参数有三个: -a -l -F. ls –a Linux上的文件以.开头的文件被系统视为隐藏文件,仅用ls命令是看 ...
- Python 入门之 Python三大器 之 装饰器
Python 入门之 Python三大器 之 装饰器 1.开放封闭原则: (1)代码扩展进行开放 任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代 ...
- Jmeter学习总结
学习内容: 1.用户定义的变量 作用:多个地方使用同一个值,且该值在不同的环境下不同,方便脚本在不同环境下运行时修改. 2.基本的HTTP请求,请求方式:get 3.传入参数为json 4.HTTP信 ...
- java流stream中的collect()方法详解
public class StreamTest { /** * stream.collect() 的本质由三个参数构成, * 1. Supplier 生产者, 返回最终结果 * 2. BiConsum ...