[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- ...
随机推荐
- golang ---JSON-ITERATOR 使用
jsoniter ( json-iterator )是一款快且灵活的 JSON 解析器 Jsoniter 是最快的 JSON 解析器.它最多能比普通的解析器快 10 倍之多, 独特的 iterator ...
- AutoCAD ObjectARX 二次开发(2020版)--3,执行ARX文件--
上一节中我们在initApp()函数中,将helloWorld()函数注册给了CAD主程序,注册指令的字符串为“Hello”. void initApp() { acedRegCmds->add ...
- OpenResty部署nginx及nginx+lua
因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件 ...
- php中的for循环和js中的for循环
php中的for循环 循环100个0 for ($i=0;$i<=100;$i++){ $pnums.='0'.","; } js中的for循环,循环31个相同的数.循环日期 ...
- webpack 打包器
创建目录mkdir demo && cd demo 产生package.json执行 npm init -y 先全局安装webpack和webpack-clinpm install w ...
- 米尔电子i.MX8开发板评测
基于 NXP 公司的i.MX8M 系列芯片的高性能开发平台 MYD-JX8MX开发板.是采用核心板(MYC-JX8MX)加底板(MYB-JX8MX)的形式,提供了 HDMI,LVDS(或 MIPI), ...
- js正则表达式(七)
一.正则表达式对象的创建方法一:使用构造函数的形式: var pattern = new RegExp('正则表达式','修饰符'); var pattern = new RegExp('hello' ...
- Django:表多对多查询、聚合分组、FQ查询、事务
1表多对多的关系查询 准备工作创建表结构 from django.db import models # Create your models here. class Publisher(models. ...
- CSS-图片整合笔记
注意点: 概念:图片整合技术( css sprite 或 精灵图).通过将多个图片融合到一张图片,然后通过CSS background 背景定位技术技巧布局网页背景 优势:减少 http iis 请求 ...
- java自定义注释及其信息提取
转自:https://xuwenjin666.iteye.com/blog/1637247 1. 自定义注解 import java.lang.annotation.Retention; import ...