HarmonyOS - 鴻蒙自定義加載框

起因:手賤造輪子的老毛病又犯了

基于:鴻蒙SDK 版本3.1.0 API9 stage模型

下載鏈接:https://gitee.com/kaiyuanguo/harmony_-demo/tree/master/

使用說明↓

 /*
    // 1.簡約方式(只支持傳入顯示類型 + 提示語) ↓
    this.hudItem.showHud(BDHudType.loading)

    setTimeout(() => { // 最好弄個(gè) 實(shí)例對象控制下 此定時(shí)器完成任務(wù)后 進(jìn)行銷毀,這就不寫了
      this.hudItem.close()
    },2000)

    this.hudItem.showHud(BDHudType.loading,'我正給嫩加載著嘞') // 自定義提示語  


    // 2.鏈?zhǔn)椒绞剑ㄈδ?+ 方便擴(kuò)展) ↓
    this.hudItem.showHudChian()
      .setDisplayType(BDHudType.success)
      .setDismisTime(2000)
      .setMsgStr('提示語')
      .setIconSrc('/components/BDHud/testSrc.gif')
      
    */

/*
 *        hudItem.showHud(BDHudType.loading)
 *       或者自定義提示語 hudItem.showHud(BDHudType.loading,'我正給嫩加載著嘞')
 *       success提示 和error提示也一樣這樣傳入自定義提示語
 *
 *        success 和 error 默認(rèn)會(huì)自動(dòng)消失 默認(rèn)2s后 自動(dòng)消失
 *       loading 默認(rèn)不自動(dòng)消失 需要用戶在外部手動(dòng)控制 hudItem.close()
 * 
 * */

代碼實(shí)現(xiàn)↓

export enum BDHudType{
  msg,      //單提示語
  loading,  //加載框
  success,  //成功提示
  error     //錯(cuò)誤提示
}

export  class BDHudClass {
  public show: boolean;   //是否顯示
  public displayType: BDHudType; //顯示類型
  public msgStr: string = '';  //提示語
  public iconSrc:string = '';
  public hudStatus:boolean = false;

  public showHud(displayType: BDHudType,msgStr?: string){
    this.hudStatus = true;
    this.show = true;
    this.displayType = displayType;

    if(displayType == BDHudType.loading){
      this.iconSrc = '/components/BDHud/loading_hud.gif'
      this.msgStr = msgStr == undefined ? '加載中':msgStr;

    }

    if(displayType == BDHudType.success){
      this.iconSrc = '/components/BDHud/success_hud.png'
      this.msgStr = msgStr == undefined ? '成功':msgStr;
    }

    if(displayType == BDHudType.error){
      this.iconSrc = '/components/BDHud/error_hud.png'
      this.msgStr = msgStr == undefined ? '錯(cuò)誤/失敗':msgStr;
    }

    if(displayType == BDHudType.msg){
      this.iconSrc = ''
      this.msgStr = msgStr == undefined ? '提示語':msgStr;
    }

  }

  // 外部調(diào)用手動(dòng)銷毀
  public close(){
    this.hudStatus = false;
    this.show = false;
  }

  // 構(gòu)造函數(shù)
  constructor(show:boolean = false, displayType:BDHudType = BDHudType.msg,msgStr: string = '',iconSrc:string = '') {
    this.show = show;
    this.displayType = displayType;
    this.msgStr = msgStr;
    this.iconSrc = iconSrc;
  }


}

@Component
export default struct BDHud{
  @Link @Watch('itemChange') hudItem:BDHudClass
  @State ZIndex:number = -1
  @State boxShow:boolean = false
  @State iconShow:boolean = false
  @State textShow:boolean = false
  private timeoutID:any

  itemChange(){
    clearTimeout(this.timeoutID);

    this.boxShow = this.hudItem.show
    this.iconShow = this.hudItem.iconSrc.length == 0 ? false:true;
    this.textShow = this.hudItem.msgStr.length == 0 ? false:true;

    if(this.boxShow == true){
      this.ZIndex = 999
    }else {
      this.ZIndex = -1;
    }

    if(this.hudItem.displayType == BDHudType.success || this.hudItem.displayType == BDHudType.error || this.hudItem.displayType == BDHudType.msg){
      var that = this
      clearTimeout(this.timeoutID);
      this.timeoutID = setTimeout(() => {
        that.boxShow = false
        that.hudItem.show = false
        that.hudItem.hudStatus = false;
      },2000)

    }

  }


  build(){
    Stack(){
      Column(){
        Image(this.hudItem.iconSrc)
          .iconImg()
          .visibility(this.iconShow ? Visibility.Visible : Visibility.None)

        Text(this.hudItem.msgStr)
          .lineHeight(28)
          .padding({top:this.iconShow ? 15:0})
          .msgText()
          .visibility(this.textShow ? Visibility.Visible : Visibility.None)

      }
      .padding({top:this.iconShow ? 18:10,bottom:15,left:15,right:15})
      .box()

    }
    .zIndex(this.ZIndex)
    .visibility(this.boxShow == true ? Visibility.Visible:Visibility.None)

  }
}

@Extend(Text) function msgText(){
  .fontColor(Color.White)
  .fontSize(15)
}

@Extend(Image) function iconImg(){
  .width(60)
  .height(60)
}

@Extend(Column) function box() {
  .constraintSize({minWidth:135,maxWidth:300})
  .backgroundColor('rgba(0,0,0,0.5)')
  .borderRadius(5)
}

參考鏈接:https://github.com/ChanggnahC/YLLoadingHUD

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容