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 ...
随机推荐
- Zabbix4.0.1使用自带模板监控Linux主机 CPU、内存、硬盘、网卡
环境: 前提已经安装好zabbix服务端.zabbix客户端, zabbix_server端ip和主机名信息: ip:192.168.1.204 hostname: www.test.com ...
- Elasticsearch如何关掉服务
1.使用head插件找到想关掉的节点进行关停 2.使用命令kill杀掉服务器的ES进程即可1.查找ES进程ps -ef | grep elastic 2.杀掉ES进程kill -9 2382(进程号) ...
- 【MM系列】SAP MM物料账在制品承担差异功能及配置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM物料账在制品承担差异功能 ...
- C++ 中的new、malloc、namespace
1,这些新引入的成员想要解决 C 语言中存在的一些问题, 2,动态内存分配: 1,C++ 中的动态内存分配: 1,C++ 中通过 new 关键字进行基于类型的动态内存申请: 1,C 语言中自身不包含动 ...
- linux下安装phpunit
安装pear 的命令如下: $ wget http://pear.php.net/go-pear.phar $ php go-pear.phar 如果报出PHP Warning: file_exis ...
- org.apache.httpcomponents:httpclient 工具类
基于httpclient 版本4.4.1 因为http连接需要三次握手,在需要频繁调用时浪费资源和时间 故采用连接池的方式连接 根据实际需要更改 连接池最大连接数.路由最大连接数 另一个需要注意的是 ...
- 40. Combination Sum II (JAVA)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- Ubuntu16.04 php7.1安装redis扩展
sudo apt install php7.1-redis //修改php配置 vi /etc/php.ini 添加extension=redis.so
- 模型验证方法——R语言
在数据分析中经常会对不同的模型做判断 一.混淆矩阵法 作用:一种比较简单的模型验证方法,可算出不同模型的预测精度 将模型的预测值与实际值组合成一个矩阵,正例一般是我们要预测的目标.真正例就是预测为正例 ...
- spring通过注解转换日期
Spring中有@DataTimeFormat和@JsonFormat @JsonFormat不要忘了加GMT+8 @DateTimeFormat要注意前台传过的日期格式是yyyy-MM-dd的形式, ...