最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-native 封裝選擇彈出框示例(試用ios&android)

 更新時(shí)間:2017年07月11日 09:13:47   作者:sybil052  
本篇文章主要介紹了react-native 封裝選擇彈出框示例(試用ios&android),具有一定的參考價(jià)值,有興趣的可以了解一下

在開發(fā) App 的時(shí)候,經(jīng)常會(huì)使用到對(duì)話框(又叫消息框、提示框、告警框)。 在web開發(fā)中經(jīng)常會(huì)用得到。今天就來介紹了一下react-native 封裝彈出框

之前看到react-native-image-picker中自帶了一個(gè)選擇器,可以選擇拍照還是圖庫,但我們的項(xiàng)目中有多處用到這個(gè)選擇彈出框,所以就自己寫了一下,最最重要的是ios和Android通用。先上動(dòng)態(tài)效果圖~

一、封裝要點(diǎn)

1.使用動(dòng)畫實(shí)現(xiàn)彈框布局及顯示隱藏效果

2.通過一個(gè)boolean值控制組件的顯示隱藏

3.彈框選項(xiàng)數(shù)組通過調(diào)用的js傳到彈框組件

4.組件選項(xiàng)的字體顏色通過調(diào)用js傳到組件,實(shí)現(xiàn)可拓展;

5.選擇選項(xiàng)回調(diào)方法

二、代碼實(shí)現(xiàn)

新建alertSelected.js

/**
 * Created by sybil052 on 2017/6/19.
 */
import React, {Component} from 'react';
import {
  StyleSheet,
  View,
  Image,
  Text,
  TouchableHighlight,
  Animated,
  Easing,
  Dimensions,
  Platform,
  TouchableOpacity
} from 'react-native';

const {width, height} = Dimensions.get('window');
const [aWidth] = [width-20];
const [left, top] = [0, 0];
const [middleLeft] = [(width - aWidth) / 2];

export default class AlertSelected extends Component {
  constructor(props) {
    super(props);
    this.state = {
      offset: new Animated.Value(0),
      opacity: new Animated.Value(0),
      title: "",
      choose0: "",
      choose1: "",
      hide: true,
      tipTextColor: '#333333',
      aHeight: 236,
    };
    this.entityList = [];//數(shù)據(jù)源
    this.callback = function () {
    };//回調(diào)方法
  }

  render() {
    if (this.state.hide) {
      return (<View />)
    } else {
      return (
        <View style={styles.container}>
          <Animated.View style={styles.mask}>
          </Animated.View>

          <Animated.View style={[{
            width: aWidth,
            height: this.state.aHeight,
            left: middleLeft,
            ...Platform.select({
              ios:{
                bottom: - 20,
              },
            }),
            alignItems: "center",
            justifyContent: "space-between",
          }, {
            transform: [{
              translateY: this.state.offset.interpolate({
                inputRange: [0, 1],
                outputRange: [height, (height - this.state.aHeight - 34)]
              }),
            }]
          }]}>
            <View style={styles.content}>
            <View style={styles.tipTitleView}>
              <Text style={styles.tipTitleText}>{this.state.title}</Text>
            </View>
            {
              this.entityList.map((item, i) => this.renderItem(item, i))
            }
            </View>
            <TouchableHighlight
              style={styles.button}
              underlayColor={'#f0f0f0'}
              onPress={this.cancel.bind(this)}
            >
              <Text style={styles.buttonText}>取消</Text>
            </TouchableHighlight>
          </Animated.View>
        </View>
      );
    }
  }

  renderItem(item, i) {
    return (
      <View style={styles.tipContentView}>
        <View style={{height: 0.5, backgroundColor: '#a9a9a9', width: aWidth}}/>
        <TouchableOpacity
        key={i}
        onPress={this.choose.bind(this, i)}
      >
          <View style={styles.item}>
            <Text style={{
              color: this.state.tipTextColor,
              fontSize: 17,
              textAlign: "center",
            }}>{item}</Text>
          </View>
        </TouchableOpacity>
      </View>
    );
  }

  componentDidMount() {
  }

  componentWillUnmount() {
    // 如果存在this.timer,則使用clearTimeout清空。
    // 如果你使用多個(gè)timer,那么用多個(gè)變量,或者用個(gè)數(shù)組來保存引用,然后逐個(gè)clear
    this.timer && clearTimeout(this.timer);
    this.chooseTimer && clearTimeout(this.chooseTimer);
  }

  //顯示動(dòng)畫
  in() {
    Animated.parallel([
      Animated.timing(
        this.state.opacity,
        {
          easing: Easing.linear,//一個(gè)用于定義曲線的漸變函數(shù)
          duration: 200,//動(dòng)畫持續(xù)的時(shí)間(單位是毫秒),默認(rèn)為200。
          toValue: 0.8,//動(dòng)畫的最終值
        }
      ),
      Animated.timing(
        this.state.offset,
        {
          easing: Easing.linear,
          duration: 200,
          toValue: 1,
        }
      )
    ]).start();
 }

  //隱藏動(dòng)畫
  out() {
    Animated.parallel([
      Animated.timing(
        this.state.opacity,
        {
          easing: Easing.linear,
          duration: 200,
          toValue: 0,
        }
      ),
      Animated.timing(
        this.state.offset,
        {
          easing: Easing.linear,
          duration: 200,
          toValue: 0,
        }
      )
    ]).start((finished) => this.setState({hide: true}));
  }

  //取消
  cancel(event) {
    if (!this.state.hide) {
      this.out();
    }
  }

  //選擇
  choose(i) {
    if (!this.state.hide) {
      this.out();
      this.chooseTimer = setTimeout(()=>{
        this.callback(i);
      }, 200);
    }
  }

 /**
 * 彈出控件,最多支持3個(gè)選項(xiàng)(包括取消)
 * titile: 標(biāo)題
 * entityList:選擇項(xiàng)數(shù)據(jù)  數(shù)組
 * tipTextColor: 字體顏色
 * callback:回調(diào)方法
 */
 show(title: string, entityList: Array, tipTextColor: string, callback: Object) {
   this.entityList = entityList;
   this.callback = callback;

   if (this.state.hide) {
     if (entityList && entityList.length > 0) {
       let len = entityList.length;
       if (len === 1) {
         this.setState({title: title, choose0: entityList[0], hide: false, tipTextColor: tipTextColor, aHeight: 180}, this.in);
       } else if (len === 2) {
         this.setState({title: title, choose0: entityList[0], choose1: entityList[1], hide: false, tipTextColor: tipTextColor, aHeight: 236}, this.in);
       }
     }
   }
 }
}

const styles = StyleSheet.create({
  container: {
    position: "absolute",
    width: width,
    height: height,
    left: left,
    top: top,
  },
  mask: {
    justifyContent: "center",
    backgroundColor: "#000000",
    opacity: 0.3,
    position: "absolute",
    width: width,
    height: height,
    left: left,
    top: top,
  },
  // 提示標(biāo)題
  tipTitleView: {
    height: 56,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#fff',
    marginLeft: 10,
    marginRight: 10
  },
  // 提示文字
  tipTitleText: {
    color: "#999999",
    fontSize: 14,
  },
  // 分割線
  tipContentView: {
    width: aWidth,
    height: 56,
    backgroundColor:'#fff',
    borderBottomLeftRadius: 5,
    borderBottomRightRadius: 5,
  },
  item:{
    width: aWidth,
    height: 56,
    backgroundColor:'#fff',
    justifyContent: 'center',
    borderRadius: 5,
  },
  button: {
    height: 57,
    backgroundColor: '#fff',
    alignSelf: 'stretch',
    justifyContent: 'center',
    borderRadius: 5,
  },
  // 取消按鈕
  buttonText: {
    fontSize: 17,
    color: "#0084ff",
    textAlign: "center",
  },
  content: {
    backgroundColor: '#fff',
    borderRadius: 5,
  }
});

三、使用方法

新建demo.js

const selectedArr = ["拍照", "圖庫"];
class Demo extends Component {
  constructor(props) {
    super(props);
    this.showAlertSelected = this.showAlertSelected.bind(this);
    this.callbackSelected = this.callbackSelected.bind(this);
  }

  showAlertSelected(){
    this.dialog.show("請(qǐng)選擇照片", selectedArr, '#333333', this.callbackSelected);
  }
  // 回調(diào)
  callbackSelected(i){
    switch (i){
      case 0: // 拍照
        this.takePhoto();
        break;
      case 1: // 圖庫
        this.pickMultiple();
        break;
    }
  }
  render() {
    return (
      <View style={stylesCommon.container}>
        <TouchableOpacity onPress={() => {this.showAlertSelected();}}>
          <View style={styles.imageBorder}>
            <Text style={styles.photoText}></Text>
          </View>
        </TouchableOpacity>
        <DialogSelected ref={(dialog)=>{
          this.dialog = dialog;
        }} /> 
      </View>
    );
  } 
}

再來一張其他界面調(diào)用該組件的效果圖~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React循環(huán)遍歷渲染數(shù)組和對(duì)象元素方式

    React循環(huán)遍歷渲染數(shù)組和對(duì)象元素方式

    這篇文章主要介紹了React循環(huán)遍歷渲染數(shù)組和對(duì)象元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Electron+React進(jìn)行通信的方法

    Electron+React進(jìn)行通信的方法

    electron其實(shí)是一個(gè)桌面應(yīng)用程序,不是一個(gè)標(biāo)準(zhǔn)的前端web程序,所有沒有什么請(qǐng)求的發(fā)生,控制臺(tái)network看不到請(qǐng)求,而是只能通過console.log去打印查看,而且通信協(xié)議使用的不是http而是gRPC協(xié)議,這篇文章主要介紹了Electron+React如何進(jìn)行通信,需要的朋友可以參考下
    2022-06-06
  • React中的Context應(yīng)用場(chǎng)景分析

    React中的Context應(yīng)用場(chǎng)景分析

    這篇文章主要介紹了React中的Context應(yīng)用場(chǎng)景分析,Context 提供了一種在組件之間共享數(shù)據(jù)的方式,而不必顯式地通過組件樹的逐層傳遞 props,通過實(shí)例代碼給大家介紹使用步驟,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • React實(shí)現(xiàn)虛擬滾動(dòng)的三種思路詳解

    React實(shí)現(xiàn)虛擬滾動(dòng)的三種思路詳解

    在??web??開發(fā)的過程中,或多或少都會(huì)遇到大列表渲染的場(chǎng)景,為了解決大列表造成的渲染壓力,便出現(xiàn)了虛擬滾動(dòng)技術(shù),本文主要介紹虛擬滾動(dòng)的三種思路,希望對(duì)大家有所幫助
    2024-04-04
  • 官方推薦react-navigation的具體使用詳解

    官方推薦react-navigation的具體使用詳解

    本篇文章主要介紹了官方推薦react-navigation的具體使用詳解,react-navigation是致力于解決導(dǎo)航卡頓,數(shù)據(jù)傳遞,Tabbar和navigator布局,支持redux。非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-05-05
  • react-router v4如何使用history控制路由跳轉(zhuǎn)詳解

    react-router v4如何使用history控制路由跳轉(zhuǎn)詳解

    這篇文章主要給大家介紹了關(guān)于react-router v4如何使用history控制路由跳轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • react-draggable實(shí)現(xiàn)拖拽功能實(shí)例詳解

    react-draggable實(shí)現(xiàn)拖拽功能實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于react-draggable實(shí)現(xiàn)拖拽功能的相關(guān)資料,React-Draggable一個(gè)使元素可拖動(dòng)的簡(jiǎn)單組件,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • React.js源碼解析setState流程

    React.js源碼解析setState流程

    這篇文章主要為大家介紹了React.js源碼解析setState流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • react?hooks深拷貝后無法保留視圖狀態(tài)解決方法

    react?hooks深拷貝后無法保留視圖狀態(tài)解決方法

    這篇文章主要為大家介紹了react?hooks深拷貝后無法保留視圖狀態(tài)解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • React事件綁定的方式詳解

    React事件綁定的方式詳解

    react事件綁定時(shí)。this并不會(huì)指向當(dāng)前DOM元素。往往使用bind來改變this指向,今天通過本文給大家介紹React事件綁定的方式,感興趣的朋友
    2021-07-07

最新評(píng)論

繁昌县| 万盛区| 叶城县| 江都市| 囊谦县| 荆门市| 深圳市| 柘荣县| 秦皇岛市| 宝丰县| 中方县| 九龙坡区| 莱州市| 哈巴河县| 碌曲县| 保德县| 云安县| 临沂市| 精河县| 海盐县| 泰和县| 安阳县| 江永县| 耒阳市| 苏尼特右旗| 镇宁| 三台县| 富民县| 崇州市| 巴南区| 壤塘县| 民丰县| 涿州市| 屏山县| 吉水县| 顺平县| 芮城县| 蓬安县| 环江| 库尔勒市| 思南县|