[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- ...
随机推荐
- 关于使用KubeSphere中的docker配置Harbor仓库http访问docker login登陆报错的解决办法
# 先进入harbor目录中,停止harbor docker-compose stop # 然后修改docker相关文件 # 第一种方式:修改/etc/docker/daemon.json { &qu ...
- Java子类方法签名相同,返回类型不同
2019年7月27日15:04:20 Java子类覆盖父类的方法,方法名字相同,参数列表相同,返回类型不同的情况: 如果子类方法返回类型是父类方法返回类型的子类,这是没问题的,否则报错. 在JAVA ...
- 转:Windows下交换CapsLock和左ctrl
Windows下交换CapsLock和左ctrlHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout中添加Scanco ...
- Redis Cluster: (error) MOVED
I have a Redis cluster with the following nodes: 192.168.0.14:6379 master (slots from 0 to 16383) ...
- 在Centos中安装.net core SDK
在Linux中运行.net core 项目必须要有.net core SDK 环境.之前配置过几次,但由于没有做总结.过了几天又配置的时候 感觉特别陌生,今天就记录一次.net core SDK 的安 ...
- 遍历切片slice,结构体struct,映射map,interface{}的属性和值
1 前言 说明:interface{}必须是前三者类型 2 代码 /** * @Author: FB * @Description: * @File: testOutput.go * @Version ...
- Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)
Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...
- .net 后台以post方式调用微信公众平台接口
public class Fresult { public int errcode { get; set; } public string errmsg { get; set; } public st ...
- React: JSX生成真实DOM结点
在上一篇文章中,我们介绍了 Babel 是如何将 JSX 代码编译成可执行代码的,随后也实现了一个自己的解析器,模拟了 Babel 编译的过程. 现在我们再来回顾一下,假定有如下业务代码: const ...
- python测试开发django-42.xadmin自定义菜单项
前言 xadmin后台的菜单项是放到一个app下的,并且里面的排序是按字母a-z排序,有时候我们需要划分多个项,需要自定义菜单列表,可以通过重写CommAdminView类实现.xadmin后台提供了 ...