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

react native仿微信PopupWindow效果的實例代碼

 更新時間:2017年08月07日 09:06:48   作者:code_xzh  
本篇文章主要介紹了react native仿微信PopupWindow效果的實例代碼,具有一定的參考價值,有興趣的可以了解一下

在原生APP開發(fā)中,相信很多開發(fā)者都會見到這種場景:點擊右上角更多的選項,彈出一個更多界面供用戶選擇。這種控件在原生開發(fā)中Android可以用PopupWindow實現(xiàn),在iOS中可以用CMPopTipView,也可以自己寫一個View實現(xiàn)。其類似的效果如下圖所示:

實現(xiàn)思路分析:

要實現(xiàn)上面的視圖,有很多種實現(xiàn)方式。前面的文章說過,要實現(xiàn)彈框相關的可以用React Native 提供的 Modal組件(Modal組件),使用Modal組件可以實現(xiàn)我們原生開發(fā)中的大多數(shù)效果。

要實現(xiàn)下拉三角,可以讓美工切一個帶下拉三角的背景,當然也可以自己通過ART實現(xiàn)(ART繪制)。對于選項卡的內(nèi)容,在原生開發(fā)中為了適應更多的場景,我們一般會選擇使用ListView組件,然后當點擊某個Item的時候獲得相應的屬性即可。為了控制Modal的顯示與消失,我們可以給Modal內(nèi)置一個isVisible: this.props.show狀態(tài)。

源碼

要實現(xiàn)上面的效果,會這涉及到三個js文件:MorePopWidows.js、Utils.js、HomeActionBar.js,按照先后順序,代碼如下:

Utils.js

import {Dimensions} from 'react-native'

const deviceH = Dimensions.get('window').height
const deviceW = Dimensions.get('window').width

const basePx = 375

export default function px2dp(px) { 
  return px * deviceW / basePx
}

MorePopWidows.js

import React from 'react'
import {
  StyleSheet,
  Platform,
  View,
  Text,
  Image,
  TouchableOpacity,
  Alert,
  Modal,
  Dimensions,
} from 'react-native'
import SpacingView from "./SpacingView";
import QRScanPage from "../home/QRScanPage";

const { width, height } = Dimensions.get('window');
import px2dp from '../util/Utils'

const mTop = px2dp(Platform.OS == "ios" ? 64 : 44)

let mwidth = 95;
let mheight = 100;
const marginTop = mTop;

export default class MorePopWidows extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      isVisible: this.props.show,
    }
    mwidth = this.props.width ;
    mheight = this.props.height ;
  }

  componentWillReceiveProps(nextProps) {
    this.setState({ isVisible: nextProps.show });
  }

  closeModal() {
    this.setState({
      isVisible: false
    });
    this.props.closeModal(false);
  }

  scan() {
    this.props.navigator.push({
      component: QRScanPage,
    })
  }

  render() {
    return (
      <View style={styles.container}>
       <Modal
         transparent={true}
         visible={this.state.isVisible}
         animationType={'fade'}
         onRequestClose={() => this.closeModal()}>
        <TouchableOpacity style={styles.container} activeOpacity={1} onPress={() => this.closeModal()}>

         <View style={styles.modal}>
          <TouchableOpacity activeOpacity={1} onPress={this.scan.bind(this)} style={styles.itemView}>
           <Image style={styles.imgStyle} source={require('../images/ic_scan_code_white.png')} />
           <Text style={styles.textStyle}>掃一掃</Text>
          </TouchableOpacity>
           <SpacingView/>
          <TouchableOpacity activeOpacity={1} onPress={() => Alert.alert('點擊了付款碼')} style={styles.itemView}>
           <Image style={styles.imgStyle} source={require('../images/ic_code_white.png')} />
           <Text style={styles.textStyle}>付款碼</Text>
          </TouchableOpacity>
         </View>
        </TouchableOpacity>
       </Modal>
      </View>
    )
  }
}
const styles = StyleSheet.create({
  container: {
    width: width,
    height: height,
  },
  modal: {
    backgroundColor: '#696969',
    width: mwidth,
    height: mheight,
    position: 'absolute',
    left: width - mwidth - 10,
    top: marginTop,
    padding: 5,
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 3,
  },
  itemView: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
  },
  textStyle: {
    color: '#fff',
    fontSize: 14,
    marginLeft: 2,
  },
  imgStyle: {
    width: 20,
    height: 20,
  }
});

最后是在代碼中使用MorePopWidows的代碼:

HomeActionBar.js

/**
 * https://github.com/facebook/react-native
 * @flow 首頁的標題欄
 */

import React, {Component} from 'react';
import {Platform, View, Dimensions, Text, StyleSheet, TouchableOpacity, Image} from 'react-native';
import SelectCityPage from '../home/SelectCityPage'
import MorePopWidows from '../component/MorePopWidows'
import px2dp from '../util/Utils'

const isIOS = Platform.OS == "ios"
const {width, height} = Dimensions.get('window')
const headH = px2dp(isIOS ? 64 : 44)

export default class HomeActionBar extends Component {

  constructor(props) {
    super(props);
    this.state = {
      showPop: false,
    }
  }

  city() {
    this.props.navigator.push({
      component: SelectCityPage,
    })
  }

  renderHeader() {
    return (
      <View >
      <View style={styles.headerStyle}>
        <TouchableOpacity style={styles.action} onPress={this.city.bind(this)}>
          <Text style={styles.text}>上海</Text>
          <Image
            source={require('../images/ic_arrow_down.png')}/>
        </TouchableOpacity>
        <TouchableOpacity style={styles.searchBar}>
          <Image source={require('../images/ic_search.png')} style={styles.iconStyle}/>
          <Text style={{fontSize: 13, color: "#666", marginLeft: 5}}>輸入商家、商品名稱</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.action} onPress={() => { this.setState({ showPop: !this.state.showPop }) }}>
          <Image style={styles.scanIcon}
              source={require('../images/ic_scan_code_white.png')}/>
          <Text style={styles.scanText}>掃碼</Text>
        </TouchableOpacity>
      </View>
        <View style={{ position: 'absolute', top: headH, left: 0, width: width, height: height }}>
          <MorePopWidows width={90} height={100} show={this.state.showPop} closeModal={(show) => {
            this.setState({showPop: show})
          }} {...this.props}/>
        </View>

      </View>
    )
  }

  render() {
    return (
      <View>
        {this.renderHeader()}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  headerStyle: {
    backgroundColor: "#06C1AE",
    height: headH,
    paddingTop: px2dp(isIOS ? 20 : 0),
    paddingHorizontal: 16,
    flexDirection: 'row',
    alignItems: 'center',
  },
  searchBar: {
    width: width * 0.65,
    height: 30,
    borderRadius: 19,
    marginLeft: 10,
    flexDirection: 'row',
    justifyContent: 'flex-start',
    alignItems: 'center',
    backgroundColor: 'white',
    alignSelf: 'center',
    paddingLeft: 10,
  },
  text: {
    fontSize: 16,
    color: '#ffffff',
    justifyContent: 'center',
  },
  iconStyle: {
    width: 22,
    height: 22,
  },
  action: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
  scanIcon: {
    width: 28,
    height: 28,
    alignItems: 'center',
    marginLeft: 10,
  },
  scanText: {
    fontSize: 14,
    color: '#ffffff',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • React改變元素樣式的操作代碼

    React改變元素樣式的操作代碼

    這篇文章主要介紹了React技巧之改變元素樣式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • React使用TypeScript的最佳實踐和技巧

    React使用TypeScript的最佳實踐和技巧

    在React項目中使用TypeScript可以顯著提高代碼的可維護性和可讀性,并提供強大的類型檢查功能,減少運行時錯誤,以下是一些優(yōu)雅地將TypeScript集成到React項目中的最佳實踐和技巧,需要的朋友可以參考下
    2024-06-06
  • 詳解如何優(yōu)雅地在React項目中使用Redux

    詳解如何優(yōu)雅地在React項目中使用Redux

    這篇文章主要介紹了詳解如何優(yōu)雅地在React項目中使用Redux,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • React中多語言的配置方式

    React中多語言的配置方式

    這篇文章主要介紹了React中多語言的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • React項目配置axios和反向代理和process.env環(huán)境配置等問題

    React項目配置axios和反向代理和process.env環(huán)境配置等問題

    這篇文章主要介紹了React項目配置axios和反向代理和process.env環(huán)境配置等問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 30行代碼實現(xiàn)React雙向綁定hook的示例代碼

    30行代碼實現(xiàn)React雙向綁定hook的示例代碼

    本文主要介紹了30行代碼實現(xiàn)React雙向綁定hook的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • React Hooks之使用useCallback和useMemo進行性能優(yōu)化方式

    React Hooks之使用useCallback和useMemo進行性能優(yōu)化方式

    這篇文章主要介紹了React Hooks之使用useCallback和useMemo進行性能優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • React實現(xiàn)pc端的彈出框效果

    React實現(xiàn)pc端的彈出框效果

    這篇文章主要為大家詳細介紹了React實現(xiàn)pc端的彈出框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • React在定時器中無法獲取狀態(tài)最新值的問題

    React在定時器中無法獲取狀態(tài)最新值的問題

    這篇文章主要介紹了React在定時器中無法獲取狀態(tài)最新值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React網(wǎng)絡請求發(fā)起方法詳細介紹

    React網(wǎng)絡請求發(fā)起方法詳細介紹

    在編程開發(fā)中,網(wǎng)絡數(shù)據(jù)請求是必不可少的,這篇文章主要介紹了React網(wǎng)絡請求發(fā)起方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-09-09

最新評論

连山| 横峰县| 双峰县| 晴隆县| 百色市| 卢湾区| 巴塘县| 穆棱市| 宜川县| 凤台县| 兰州市| 南澳县| 阿合奇县| 玉林市| 苗栗市| 兰西县| 日土县| 莲花县| 武胜县| 大城县| 济阳县| 正阳县| 河北省| 秦皇岛市| 郴州市| 宜良县| 体育| 土默特左旗| 乐山市| 左贡县| 兴文县| 蒙自县| 佛山市| 西峡县| 淮滨县| 含山县| 武夷山市| 诸城市| 东宁县| 甘德县| 东方市|