国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

ReactNative如何實(shí)現Toast

發(fā)布時(shí)間:2021-07-27 11:48 來(lái)源:億速云 閱讀:0 作者:小新 欄目: web開(kāi)發(fā)

這篇文章將為大家詳細講解有關(guān)ReactNative如何實(shí)現Toast,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

定義組件

import React, {Component} from 'react';
import {
  StyleSheet,
  View,
  Easing,
  Dimensions,
  Text,
  Animated
} from 'react-native';
import PropTypes from 'prop-types';
import Toast from "./index";
const {width, height} = Dimensions.get("window");
const viewHeight = 35;
class ToastView extends Component {
  static propTypes = {
    message:PropTypes.string,
  };
  dismissHandler = null;

  constructor(props) {
    super(props);
    this.state = {
      message: props.message !== undefined ? props.message : ''
    }
  }

  render() {
    return (
      <View style={styles.container} pointerEvents='none'>
        <Animated.View style={[styles.textContainer]}><Text
          style={styles.defaultText}>{this.state.message}</Text></Animated.View>
      </View>
    )
  }
  componentDidMount() {
    this.timingDismiss()
  }

  componentWillUnmount() {
    clearTimeout(this.dismissHandler)
  }


  timingDismiss = () => {
    this.dismissHandler = setTimeout(() => {
      this.onDismiss()
    }, 1000)
  };

  onDismiss = () => {
    if (this.props.onDismiss) {
      this.props.onDismiss()
    }
  }
}

const styles = StyleSheet.create({
  textContainer: {
    backgroundColor: 'rgba(0,0,0,.6)',
    borderRadius: 8,
    padding: 10,
    bottom:height/8,
    maxWidth: width / 2,
    alignSelf: "flex-end",
  },
  defaultText: {
    color: "#FFF",
    fontSize: 15,
  },
  container: {
    position: "absolute",
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    flexDirection: "row",
    justifyContent: "center",
  }
});
export default ToastView

首先導入我們必須的基礎組件以及API,我們自定義組件都需要繼承它,Dimensions用于實(shí)現動(dòng)畫(huà),Easing用于設置動(dòng)畫(huà)的軌跡運行效果,PropTypes用于對屬性類(lèi)型進(jìn)行定義。

render方法是我們定義組件渲染的入口,最外層view使用position為absolute,并設置left,right,top,bottom設置為0,使其占滿(mǎn)屏幕,這樣使用Toast顯示期間不讓界面監聽(tīng)點(diǎn)擊事件。內層View是Toast顯示的黑框容器,backgroundColor屬性設置rgba形式,顏色為黑色透明度為0.6。并設置圓角以及最大寬度為屏幕寬度的一半。然后就是Text組件用于顯示具體的提示信息。

我們還看到propTypes用于限定屬性message的類(lèi)型為string。constructor是我們組件的構造方法,有一個(gè)props參數,此參數為傳遞過(guò)來(lái)的一些屬性。需要注意,構造方法中首先要調用super(props),否則報錯,在此處,我將傳遞來(lái)的值設置到了state中。

對于Toast,顯示一會(huì )兒自動(dòng)消失,我們可以通過(guò)setTimeout實(shí)現這個(gè)效果,在componentDidMount調用此方法,此處設置時(shí)間為1000ms。然后將隱藏毀掉暴露出去。當我們使用setTimeout時(shí)還需要在組件卸載時(shí)清除定時(shí)器。組件卸載時(shí)回調的時(shí)componentWillUnmount。所以在此處清除定時(shí)器。

實(shí)現動(dòng)畫(huà)效果

在上面我們實(shí)現了Toast的效果,但是顯示和隱藏都沒(méi)有過(guò)度動(dòng)畫(huà),略顯生硬。那么我們加一些平移和透明度的動(dòng)畫(huà),然后對componentDidMount修改實(shí)現動(dòng)畫(huà)效果

在組件中增加兩個(gè)變量

moveAnim = new Animated.Value(height / 12);
  opacityAnim = new Animated.Value(0);

在之前內層view的樣式中,設置的bottom是height/8。我們此處將view樣式設置如下

style={[styles.textContainer, {bottom: this.moveAnim, opacity: this.opacityAnim}]}

然后修改componentDidMount

componentDidMount() {
    Animated.timing(
      this.moveAnim,
      {
        toValue: height / 8,
        duration: 80,
        easing: Easing.ease
      },
    ).start(this.timingDismiss);
    Animated.timing(
      this.opacityAnim,
      {
        toValue: 1,
        duration: 100,
        easing: Easing.linear
      },
    ).start();
  }

也就是bottom顯示時(shí)從height/12到height/8移動(dòng),時(shí)間是80ms,透明度從0到1轉變執行時(shí)間100ms。在上面我們看到有個(gè)easing屬性,該屬性傳的是動(dòng)畫(huà)執行的曲線(xiàn)速度,可以自己實(shí)現,在Easing API中已經(jīng)有多種不同的效果。大家可以自己去看看實(shí)現,源碼地址是 https://github.com/facebook/react-native/blob/master/Libraries/Animated/src/Easing.js ,自己實(shí)現的話(huà)直接給一個(gè)計算函數就可以,可以自己去看模仿。

定義顯示時(shí)間

在前面我們設置Toast顯示1000ms,我們對顯示時(shí)間進(jìn)行自定義,限定類(lèi)型number,

time: PropTypes.number

在構造方法中對時(shí)間的處理

time: props.time && props.time < 1500 ? Toast.SHORT : Toast.LONG,

在此處我對時(shí)間顯示處理為SHORT和LONG兩種值了,當然你可以自己處理為想要的效果。

然后只需要修改timingDismiss中的時(shí)間1000,寫(xiě)為this.state.time就可以了。

組件更新

當組件已經(jīng)存在時(shí)再次更新屬性時(shí),我們需要對此進(jìn)行處理,更新state中的message和time,并清除定時(shí)器,重新定時(shí)。

componentWillReceiveProps(nextProps) {
   this.setState({
      message: nextProps.message !== undefined ? nextProps.message : '',
      time: nextProps.time && nextProps.time < 1500 ? Toast.SHORT : Toast.LONG,
    })
    clearTimeout(this.dismissHandler)
    this.timingDismiss()
  }

組件注冊

為了我們的定義的組件以API的形式調用,而不是寫(xiě)在render方法中,所以我們定義一個(gè)跟組件

import React, {Component} from "react";
import {StyleSheet, AppRegistry, View, Text} from 'react-native';
viewRoot = null;
class RootView extends Component {
  constructor(props) {
    super(props);
    console.log("constructor:setToast")
    viewRoot = this;
    this.state = {
      view: null,
    }
  }

  render() {
    console.log("RootView");
    return (<View style={styles.rootView} pointerEvents="box-none">
      {this.state.view}
    </View>)
  }
  static setView = (view) => {
//此處不能使用this.setState
    viewRoot.setState({view: view})
  };
}

const originRegister = AppRegistry.registerComponent;
AppRegistry.registerComponent = (appKey, component) => {
  return originRegister(appKey, function () {
    const OriginAppComponent = component();
    return class extends Component {

      render() {
        return (
          <View style={styles.container}>
            <OriginAppComponent/>
            <RootView/>
          </View>
        );
      };
    };
  });
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    position: 'relative',
  },
  rootView: {
    position: "absolute",
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    flexDirection: "row",
    justifyContent: "center",
  }
});
export default RootView

RootView就是我們定義的根組件,實(shí)現如上,通過(guò)AppRegistry.registerComponent注冊。

包裝供外部調用

import React, {
  Component,
} from 'react';
import RootView from '../RootView'
import ToastView from './ToastView'
class Toast {
  static LONG = 2000;
  static SHORT = 1000;

  static show(msg) {
    RootView.setView(<ToastView
      message={msg}
      onDismiss={() => {
        RootView.setView()
      }}/>)
  }

  static show(msg, time) {
    RootView.setView(<ToastView
      message={msg}
      time={time}
      onDismiss={() => {
        RootView.setView()
      }}/>)
  }
}
export default Toast

Toast中定義兩個(gè)static變量,表示顯示的時(shí)間供外部使用。然后提供兩個(gè)static方法,方法中調用RootView的setView方法將ToastView設置到根view。

使用

首先導入上面的Toast,然后通過(guò)下面方法調用

Toast.show("測試,我是Toast");
          //能設置顯示時(shí)間的Toast
          Toast.show("測試",Toast.LONG);

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

国产成A人亚洲精V品无码性色| 伊人久久无码大香线蕉综合| 精品熟女碰碰人人A久久| 天堂网www在线资源网| 四虎国产精品成人| 亚洲最大无码AV网址|