MJRefresh自訂重新整理動畫,

來源:互聯網
上載者:User

MJRefresh自訂重新整理動畫,
【一】常見用法最原始的用法,耦合度低,但是不能統一管理。我們需要在每一個控制器都寫以下代碼,很繁瑣,以後項目修改起來更繁瑣,得一個控制器一個控制器的去定位、修改。

1.1 使用預設重新整理(耦合度底,但是想統一修改起來特別麻煩)

self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{        //在這裡執行重新整理操作    }];self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(重新整理方法)];

1.2 自訂重新整理(耦合度底,但是想統一修改起來特別麻煩)

//1.2.1建立一個自訂重新整理    MJRefreshNormalHeader *customRef =[MJRefreshNormalHeader headerWithRefreshingBlock:^{        //1.2.2在這裡執行重新整理操作    }];    //1.2.3 自訂重新整理狀態文字    [customRef setTitle:@"普通閑置狀態" forState:MJRefreshStateIdle];    [customRef setTitle:@"鬆開就可以進行重新整理的狀態" forState:MJRefreshStatePulling];    [customRef setTitle:@"正在重新整理中的狀態" forState:MJRefreshStateRefreshing];    [customRef setTitle:@"即將重新整理的狀態" forState:MJRefreshStateWillRefresh];    [customRef setTitle:@"所有資料載入完畢,沒有更多的資料了" forState:MJRefreshStateNoMoreData];    //1.2.4添加下拉重新整理    self.tableView.mj_header = customRef;    //一些其他屬性設定    /*    // 設定字型    customRef.stateLabel.font = [UIFont systemFontOfSize:15];    customRef.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];    // 設定顏色    customRef.stateLabel.textColor = [UIColor redColor];    customRef.lastUpdatedTimeLabel.textColor = [UIColor blueColor];    // 隱藏時間    customRef.lastUpdatedTimeLabel.hidden = YES;    // 隱藏狀態    customRef.stateLabel.hidden = YES;     // 設定自動切換透明度(在導覽列下面自動隱藏)     customRef.automaticallyChangeAlpha = YES;     */
【二】正確用法(統一管理項目所有下拉重新整理控制項)

一共有6種重新整理對象(圖片末尾帶紅色的都是)。

假如我們想實現預設的下拉重新整理,
【2.1】只要建立一個對象,
繼承自MJRefreshNormalHeader

#import "MJRefreshNormalHeader.h"@interface HSNormalHeader : MJRefreshNormalHeader@end

【2.2】然後重寫方法- (void)prepare,重寫後記得實現[super prepare]

#import "HSNormalHeader.h"@implementation HSNormalHeader#pragma mark - 重寫父類的方法- (void)prepare{    [super prepare];    //所有的自訂東西都放在這裡    [self setTitle:@"普通閑置狀態" forState:MJRefreshStateIdle];    [self setTitle:@"鬆開就可以進行重新整理的狀態" forState:MJRefreshStatePulling];    [self setTitle:@"正在重新整理中的狀態" forState:MJRefreshStateRefreshing];    [self setTitle:@"即將重新整理的狀態" forState:MJRefreshStateWillRefresh];    [self setTitle:@"所有資料載入完畢,沒有更多的資料了" forState:MJRefreshStateNoMoreData];    //一些其他屬性設定    /*     // 設定字型     self.stateLabel.font = [UIFont systemFontOfSize:15];     self.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];     // 設定顏色     self.stateLabel.textColor = [UIColor redColor];     self.lastUpdatedTimeLabel.textColor = [UIColor blueColor];     // 隱藏時間     self.lastUpdatedTimeLabel.hidden = YES;     // 隱藏狀態     self.stateLabel.hidden = YES;     // 設定自動切換透明度(在導覽列下面自動隱藏)     self.automaticallyChangeAlpha = YES;     */}//如果需要自己重新布局子控制項- (void)placeSubviews{    [super placeSubviews];    //如果需要自己重新布局子控制項,請在這裡設定    //箭頭//    self.arrowView.center =}@end

然後我們再對應的控制器實現重新整理只要這樣既可:
【2.3】引入自訂對象,比如叫HSNormalHeader(繼承自MJRefreshNormalHeader),

#import "HSNormalHeader.h"

【2.4】在需要下拉重新整理的控制器這樣實現既可

self.tableView.mj_header = [HSNormalHeader headerWithRefreshingBlock:^{        //重新整理請求    }];

同理,對應的下拉動畫只需建立對象繼承自MJRefreshGifHeader

#pragma mark - 重寫父類的方法- (void)prepare{    [super prepare];    // 設定普通狀態的動畫圖片    NSMutableArray *idleImages = [NSMutableArray array];    for (NSUInteger i = 1; i<=60; i++) {        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];        [idleImages addObject:image];    }    [self setImages:idleImages forState:MJRefreshStateIdle];    // 設定即將重新整理狀態的動畫圖片(一鬆開就會重新整理的狀態)    NSMutableArray *refreshingImages = [NSMutableArray array];    for (NSUInteger i = 1; i<=3; i++) {        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd", i]];        [refreshingImages addObject:image];    }    [self setImages:refreshingImages forState:MJRefreshStatePulling];    // 設定正在重新整理狀態的動畫圖片    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];    //隱藏時間    self.lastUpdatedTimeLabel.hidden = YES;    //隱藏狀態    self.stateLabel.hidden = YES;}

至此,可以說MJRefresh的常用主要功能都已經被你掌握完畢,如需掌握更多請去參考MJ的地址https://github.com/CoderMJLee/MJRefresh

本案例的demo地址為https://github.com/wolfhous/HSUpdateApp註:裡面還包含一個的自動檢測更新,大家覺得好用請加星哦!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.