[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- ...
随机推荐
- Django开发之登陆和登出
使用django自带的验证模块 1.首先使用python manage.py startapp models为当前项目添加一个应用. 2.在setting.py中INSTALLED_APPS后面添加' ...
- k8s 运行应用
一.deployment 创建过程 kubect创建deployment —> deployment 创建ReplicaSet—>根据ReplicaSet 创建Pod 命名方式 relic ...
- MyBatis返回结果类型为Boolean
问题描述: 在使用MyBatis时,有时需要检查某个记录是否存在数据库中,然后根据其返回的布尔值true or false,来进行逻辑判断.那怎么做呢? 解决方案: 如检测某个手机号是否 ...
- MVC Filter的使用方法
相信对权限过滤大家伙都不陌生 用户要访问一个页面时 先对其权限进行判断并进行相应的处理动作 在webform中 最直接也是最原始的办法就是 在page_load事件中所有代码之前 先执行一个权限判断的 ...
- Java自学-类和对象 构造方法
怎么使用 Java 构造方法? 通过一个类创建一个对象,这个过程叫做实例化 实例化是通过调用构造方法(又叫做构造器)实现的 步骤 1 : 什么是构造方法 方法名和类名一样(包括大小写) 没有返回类型 ...
- css3实现半圆和圆效果
在css2中,如果需要失效一些圆角或者半圆等等效果,一般是要通过ps等软件来处理的,在CSS3中,则不需要了,只需要通过border-radius就可以实现,大大方便了开发的效率. 无论圆角.圆弧.实 ...
- 【转】StackTraceElement获取方法调用栈的信息
本文链接:https://blog.csdn.net/hp910315/article/details/52702199 一.什么是StackTrace StackTrace(堆栈轨迹)存放的就是方法 ...
- base64的使用
import base64with open("test.jpg", "rb") as f: file = f.read()file_base64 = base ...
- 【故障解决】OGG-00446 错误解决
[故障解决]OGG-00446 Could not find archived log for sequence 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和 ...
- index.jsp乱码问题的解决
我们在做java项目的时候,都会有个首页,一般就是index.jsp,然后在index.jsp中引入相关的文件,一般也是引入打包过后的相关资源文件. 当index.jsp上面的中文出现乱码的时候,就需 ...