Weex組件:<loading><refresh>

官方手冊(cè)-loading

官方手冊(cè)-refresh

  • <loading><scroller><list> 提供上拉加載功能。 是<scroller><list>的子組件惊豺,且只能在被<scroller><list> 包含時(shí)才能被正確的渲染。

  • <refresh><scroller><list> 提供下拉加載功能禽作。是<scroller><list> 的子組件尸昧,且只能在被 <scroller><list> 包含時(shí)才能被正確的渲染。

子組件

  • <text>:文字
  • <image>:icon
  • <loading-indicator>: 默認(rèn)的動(dòng)畫效果旷偿。

特性

  • display {string}:可選值為show 或者 hide烹俗,僅隱藏 <indicator>,其他子組件依然可見萍程,loading 事件仍會(huì)被觸發(fā)幢妄。

事件

  • loading:加載時(shí)被觸發(fā)。

  • refresh:被下拉時(shí)觸發(fā)茫负。

示例

<template>
  <scroller class="scroller">
    <div class="cell" v-for="num in lists">
      <div class="panel">
        <text class="text">{{num}}</text>
      </div>
    </div>
    <loading class="loading" @loading="onloading" :display="showLoading">
      <text class="indicator">Loading ...</text>
    </loading>
  </scroller>
</template>
<script>
  const modal = weex.requireModule('modal')
  const LOADMORE_COUNT = 4
  export default {
    data () {
      return {
        showLoading: 'hide',
        lists: [1, 2, 3, 4, 5]
      }
    },
    methods: {
      onloading (event) {
        modal.toast({ message: 'loading', duration: 1 })
        this.showLoading = 'show'
        setTimeout(() => {
          const length = this.lists.length
          for (let i = length; i < length + LOADMORE_COUNT; ++i) {
            this.lists.push(i + 1)
          }
          this.showLoading = 'hide'
        }, 1500)
      }
    }
  }
</script>
<style scoped>
  .panel {
    width: 600px;
    height: 250px;
    margin-left: 75px;
    margin-top: 35px;
    margin-bottom: 35px;
    flex-direction: column;
    justify-content: center;
    border-width: 2px;
    border-style: solid;
    border-color: #DDDDDD;
    background-color: #F5F5F5;
  }
  .text {
    font-size: 50px;
    text-align: center;
    color: #41B883;
  }
  .loading {
    justify-content: center;
  }
  .indicator {
    color: #888888;
    font-size: 42px;
    padding-top: 20px;
    padding-bottom: 20px;
    text-align: center;
  }
</style>

SDK源碼

  • 組件類:WXLoadingComponent
[self registerComponent:@"loading" withClass:NSClassFromString(@"WXLoadingComponent")];
  • loading-indicator是內(nèi)建的子組件
@property (nonatomic, weak) WXLoadingIndicator *indicator;
  • 至于<text><image>蕉鸳,在源碼中并沒有涉及,是組件基類提供的嵌套功能朽褪。加入其它的組件比如<switch>也未嘗不可置吓,只是從語義上加入<text><image>作為子組件比較有意義。

  • 通過一個(gè)邏輯變量控制缔赠,只處理loading事件

@property (nonatomic) BOOL loadingEvent;
- (void)addEvent:(NSString *)eventName
{
    if ([eventName isEqualToString:@"loading"]) {
        _loadingEvent = YES;
    }
}

- (void)removeEvent:(NSString *)eventName
{
    if ([eventName isEqualToString:@"loading"]) {
        _loadingEvent = NO;
    }
}

- (void)loading
{
    if (!_loadingEvent)
        return;
    
    [self fireEvent:@"loading" params:nil];
}
  • 內(nèi)部用了一個(gè)邏輯變量存儲(chǔ)display的設(shè)置衍锚,控制“小轉(zhuǎn)轉(zhuǎn)”的顯示和隱藏
@property (nonatomic) BOOL displayState;
if (attributes[@"display"]) {
    if ([attributes[@"display"] isEqualToString:@"show"]) {
        _displayState = YES;
    } else if ([attributes[@"display"] isEqualToString:@"hide"]){
        _displayState = NO;
    } else {
        WXLogError(@"");
    }
}
- (void)setDisplay
{
    id<WXScrollerProtocol> scrollerProtocol = [self ancestorScroller];
    if (scrollerProtocol == nil || !_initFinished)
        return;
    WXComponent *scroller = (WXComponent*)scrollerProtocol;
    CGPoint contentOffset = [scrollerProtocol contentOffset];
    if (_displayState) {
        contentOffset.y = [scrollerProtocol contentSize].height - scroller.calculatedFrame.size.height + self.calculatedFrame.size.height;
        [scrollerProtocol setContentOffset:contentOffset animated:NO];
        [_indicator start];
    } else {
        _displayState = NO;
        contentOffset.y = contentOffset.y - self.calculatedFrame.size.height;
        [scrollerProtocol setContentOffset:contentOffset animated:YES];
        [_indicator stop];
    }
}
  • 至于只能在<scroller> <list>等組件中使用“小轉(zhuǎn)轉(zhuǎn)”,是用了父類的一個(gè)內(nèi)部函數(shù):判斷是否遵循WXScrollerProtocol協(xié)議
- (id<WXScrollerProtocol>)ancestorScroller
{
    if(!_ancestorScroller){
        WXComponent *supercomponent = self.supercomponent;
        while (supercomponent) {
            if([supercomponent conformsToProtocol:@protocol(WXScrollerProtocol)]){
                _ancestorScroller = (id<WXScrollerProtocol>)supercomponent;
                break;
            }
            supercomponent = supercomponent.supercomponent;
        }
    }
    
    return _ancestorScroller;
}
  • WXRefreshComponent的實(shí)現(xiàn)思路跟這個(gè)非常類似

小轉(zhuǎn)轉(zhuǎn)

  • 手冊(cè)中沒有<loading-indicator>標(biāo)簽嗤堰,但是在SDK中已經(jīng)注冊(cè)戴质,也是一個(gè)組件,應(yīng)該是可以用的踢匣。只是從語義上來說沒有實(shí)際意義告匠。
[self registerComponent:@"loading-indicator" withClass:NSClassFromString(@"WXLoadingIndicator")];
  • 跟標(biāo)簽indicator>是不同的,兩者對(duì)應(yīng)的實(shí)現(xiàn)類不一樣离唬。這個(gè)是輪播的指示器后专,那個(gè)劃一下圖片,會(huì)動(dòng)的“小點(diǎn)點(diǎn)”输莺。而這個(gè)是“小轉(zhuǎn)轉(zhuǎn)”戚哎。
[self registerComponent:@"indicator" withClass:NSClassFromString(@"WXIndicatorComponent")];
  • 采用了系統(tǒng)控件實(shí)現(xiàn)
@interface WXLoadingIndicator()
@property (nonatomic, strong) UIActivityIndicatorView* indicator;
@end
  • 可以設(shè)置顏色
if (styles[@"color"]) {
    [self setColor:[WXConvert UIColor:styles[@"color"]]];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末裸诽,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子型凳,更是在濱河造成了極大的恐慌丈冬,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甘畅,死亡現(xiàn)場(chǎng)離奇詭異埂蕊,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)疏唾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門蓄氧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人荸实,你說我怎么就攤上這事匀们。” “怎么了准给?”我有些...
    開封第一講書人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)重抖。 經(jīng)常有香客問我露氮,道長(zhǎng),這世上最難降的妖魔是什么钟沛? 我笑而不...
    開封第一講書人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任畔规,我火速辦了婚禮,結(jié)果婚禮上恨统,老公的妹妹穿的比我還像新娘叁扫。我一直安慰自己,他們只是感情好畜埋,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開白布莫绣。 她就那樣靜靜地躺著,像睡著了一般悠鞍。 火紅的嫁衣襯著肌膚如雪对室。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評(píng)論 1 289
  • 那天咖祭,我揣著相機(jī)與錄音掩宜,去河邊找鬼。 笑死么翰,一個(gè)胖子當(dāng)著我的面吹牛牺汤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浩嫌,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼檐迟,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼戴已!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起锅减,我...
    開封第一講書人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤糖儡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后怔匣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體握联,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年每瞒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了金闽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡剿骨,死狀恐怖代芜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情浓利,我是刑警寧澤挤庇,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站贷掖,受9級(jí)特大地震影響嫡秕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苹威,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一昆咽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧牙甫,春花似錦掷酗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至脏答,卻和暖如春糕殉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背殖告。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來泰國(guó)打工阿蝶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人黄绩。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓羡洁,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爽丹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子筑煮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • 在學(xué)習(xí)weex的過程中看到了常用標(biāo)簽相關(guān)的內(nèi)容辛蚊,為了自己以后能夠快速查閱特整理出此文檔。 a 簡(jiǎn)介組件定義了指向某...
    TyroneTang閱讀 4,649評(píng)論 1 3
  • afinalAfinal是一個(gè)android的ioc真仲,orm框架 https://github.com/yangf...
    passiontim閱讀 15,406評(píng)論 2 45
  • 本節(jié)任務(wù) 學(xué)會(huì)使用上拉加載更多組件<loading> 這個(gè)組件比較常用務(wù)必要掌握 接下來我?guī)Т蠹覍懸粋€(gè)這樣的頁面 ...
    酷走天涯閱讀 1,727評(píng)論 0 1
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫袋马、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,059評(píng)論 4 62
  • iOS黑科技之(AVFoundation)動(dòng)態(tài)人臉識(shí)別(二) 上一篇介紹了Core Image實(shí)現(xiàn)的靜態(tài)人臉識(shí)別[...
    TitanCoder閱讀 6,102評(píng)論 5 6