[RN] React Native 幻灯片效果 Banner
[RN] React Native 幻灯片效果 Banner
1、定义Banner
import React, {Component} from 'react';
import {Image, ScrollView, StyleSheet, Text, View,} from 'react-native';
var banner = [
"http://a.hiphotos.baidu.com/image/pic/item/ac4bd11373f08202e2518d6d45fbfbedaa641ba4.jpg",
"http://e.hiphotos.baidu.com/image/pic/item/0df3d7ca7bcb0a467e97177b6563f6246b60af3f.jpg",
"http://g.hiphotos.baidu.com/image/pic/item/b8014a90f603738d87dff378bd1bb051f919ecd7.jpg",
"http://b.hiphotos.baidu.com/image/pic/item/58ee3d6d55fbb2fb12adba45414a20a44723dce7.jpg",
];
//屏幕信息
var dimensions = require('Dimensions');
//获取屏幕的宽度和高度
var {width} = dimensions.get('window');
class Banner extends Component {
static defaultProps = {
//定时器的间隔时间
duration:
};
constructor(props) {
super(props);
this.state = {
//当前显示的下标
position: ,
}
}
//绘制完成,开启定时器
componentDidMount() {
this.startTimer();
}
startTimer() {
//1.拿到ScrollView
var scrollView = this.refs.scrollView;
this.timer = setInterval(() => {
//设置圆点的下标
var curr = this.state.position;
if (curr + > banner.length - ) {
curr = ;
} else {
curr++;
}
//更新状态机,更新当前下标
this.setState({
position: curr,
});
//滚动ScrollView,1.求出水平方向的平移量 offsetX = curr * width
scrollView.scrollTo({x: curr * width, y: , animated: true})
}, this.props.duration);
}
render() {
return (
<View style={styles.container}>
<ScrollView
ref="scrollView"
horizontal={true}
showsHorizontalScrollIndicator={false}
pagingEnabled={true}//自动分页
//滚动动画结束时调用此函数。一帧滚动结束
onMomentumScrollEnd={(v) => this.onAnimationEnd(v)}
//手指按下的时候,停止计时器
onTouchStart={() => clearInterval(this.timer)}>
{/*显示轮播图的图片内容*/}
{this.getImages()}
</ScrollView>
{/*生成底部的圆点指示器*/}
<View style={styles.indicator}>
{this.getIndicators()}
</View>
</View>
);
}
//获取轮播图显示的图片
getImages() {
var images = [];
for (var i = ; i < banner.length; i++) {
images.push(
<View key={i}>
{<Image source={{uri: banner[i]}} style={styles.image}/>}
</View>
);
}
return images;
}
//获取左下角的4个圆点
getIndicators() {
var circles = [];
for (var i = ; i < banner.length; i++) {
circles.push(
<Text key={i}
style={i === this.state.position ? styles.selected : styles.unselected}>•</Text>//• html转义字符
);
}
return circles;
}
//重写这个函数,系统已有的函数
onAnimationEnd(v) {
//1.求出水平方向的偏移量
var offsetX = v.nativeEvent.contentOffset.x;
//2.根据偏移量求出当前的页数 width为图片的宽度(banner的宽度 )
var position = Math.round(offsetX / width);
//3.更新状态机, 刷新圆点
this.setState({
position: position
});
this.startTimer();
}
//结束计时器
componentWillUnmount(nextProps, nextState) {
clearInterval(this.timer);
}
}
const styles = StyleSheet.create({
container: {
marginTop: ,
},
//底部指示器的样式
indicator: {
width: width,
height: ,
position: 'absolute',
bottom: ,
backgroundColor: 'rgba(0,0,0,0.5)',
flexDirection: 'row',
justifyContent:"center",
alignItems: 'center',
},
image: {
width: width,
height: ,
},
selected: {
marginLeft: ,
fontSize: ,
color: '#5CB85C'
},
unselected: {
marginLeft: ,
fontSize: ,
color: 'white'
}
});
module.exports = Banner;
2、调用App.js
import React, {Component} from 'react';
import {Text, View} from 'react-native';
var Banner = require('./Banner');
export default class App extends Component<Props> {
render() {
return (
<View style={{flex: }}>
<Banner/>
{/*占满屏幕剩余空间 父View必须设置 flex的值(充满屏幕)*/}
<View style={{
justifyContent: 'center',
alignItems: 'center',
flex: ,
}}>
<Text>
我是一个会自动轮播的Banner
</Text>
</View>
</View>
);
}
}
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10817577.html
转载请注明出处!谢谢~~
[RN] React Native 幻灯片效果 Banner的更多相关文章
- [RN] React Native 实现图片预览
[RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...
- [RN] React Native 下实现底部标签(支持滑动切换)
上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...
- [RN] React Native 常见基本问题归纳总结
[RN] React Native 常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...
- [RN] React Native 关闭所有黄色警告
[RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...
- [RN] React Native 常用命令行
[RN] React Native 常用命令行 1.查看当前版本 react-native --version 或 react-native -v 2.创建指定版本的React Native项目 1) ...
- [RN] React Native 实现 类似QQ 登陆页面
[RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...
- [RN] React Native 中使用 stickyHeaderIndices 实现 ScrollView 的吸顶效果
React Native中,ScrollView组件可以使用 stickyHeaderIndices 轻松实现 sticky 效果. 例如下面代码中: <ScrollView showsVert ...
- [RN] React Native 头部 滑动吸顶效果的实现
React Native 头部 滑动吸顶效果的实现 效果如下图所示: 实现方法: 一.吸顶组件封装 StickyHeader .js import * as React from 'react'; i ...
- [RN] React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页头部 效果
React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页效果 效果如下: 一.安装依赖 npm install react-native- ...
随机推荐
- HBase 系列(一)—— HBase 简介
一.Hadoop的局限 HBase 是一个构建在 Hadoop 文件系统之上的面向列的数据库管理系统. 要想明白为什么产生 HBase,就需要先了解一下 Hadoop 存在的限制?Hadoop 可以通 ...
- 超全、超详的Spring Boot配置讲解笔记
springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些默认配置进行修改. application.properties application.yml 这 ...
- wind安装Jenkins+sonar+jdk
最近公司在用Jenkins持续集成软件,自己研究的头痛,而且还是和C#项目融合到一起的,网上看到的都是Java的,我自己配了一套和C#的,和你们分享. Jenkins是一个开源软件项目,旨在提供一个开 ...
- IPv4如何转换为IPv6?
ipv6已经逐渐在应用,现在已经有很多的运营商支持ipv6,前天我们也发布了如何让电脑使用ipv6地址?有很多朋友在问?ipv6有什么作用,它的表示方式是什么,今天我们来一起来详细了解下ipv6相关计 ...
- 【MySQL】数据库中间件Atlas
1.介绍 Atlas 是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改 ...
- 《区块链DAPP开发入门、代码实现、场景应用》笔记5——区块链福利彩票的设计
笔者一直强调,一定要利用区块链的特点来解决行业存在的问题,并且该问题最好用区块链解决或者说只能用区块链解决.彩票行业就是个例子. 在讲解代码之前,首先讲解一下业务设计,如图6.15所示. 图6.15 ...
- Java -- springboot 配置 freemarker
1.添加依赖 org.springframework.boot spring-boot-starter-freemarker 2.配置application.properties spring.fre ...
- 肖哥HCNP-学前准备篇笔记
HCNA:助理 HCNP:工程师 HCIE:专家 vmvare workstation 1.安装 2.创建新的虚拟机-->典型-->稍后安装系统-->选择系统模式-->选择位置 ...
- Centos7源码部署apache/httpd服务
httpd:是一个提供网站服务的程序 监听端口:80 环境准备: Linux CentOS7.3系统 使用一台服务端,一台客户端即可: 一.安装httpd 1:安装 [root@localhost ~ ...
- C语言基础知识-运算符与表达式
C语言基础知识-运算符与表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用运算符分类 1>.算术运算符 用于处理四则运算. 2>.赋值运算符 用于将表达式的 ...