iOS重新整理第三方MJRefresh的基本使用

來源:互聯網
上載者:User

標籤:

iOS開發中最好用的重新整理第三方架構 MJRefresh

GitHub : https://github.com/CoderMJLee/MJRefresh

UIRefreshControl的介紹

1,UIRefresh是蘋果內建的重新整理控制項
2,支援iOS6.0之後的版本
3,一旦被分配到一個UITableViewController,控制項的架構是自動管理

UIRefreshControl基本使用
// 重新整理中得狀態判斷,唯讀屬性,根據狀態可做一些自訂的事情@property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing;//執行個體化對象(裡面有init,UIRefreshControl的初始化) UIRefreshControl *control = [[UIRefreshControl alloc] init]; //設定UIRefreshControl控制項的顏色(菊花和文字) control.tintColor = [UIColor redColor]; //添加到tableView中,預設尺寸和位置都已經設定好 [self.tableView addSubview:control]; // 下拉重新整理文字描述,自訂 @property (nonatomic, retain) NSAttributedString *attributedTitle// 開始重新整理- (void)beginRefreshing NS_AVAILABLE_IOS(6_0);// 結束重新整理,在確定獲得想要的載入資料之後調用- (void)endRefreshing NS_AVAILABLE_IOS(6_0);
MJRefresh的介紹

MJRefresh是繼承於UIView的自訂重新整理控制項
支援哪些控制項的重新整理?
UIScrollView、UITableView、UICollectionView、UIWebView
從iOS9之後,用MJRefresh架構的時候使用它的屬性和方法,大部分需要加入mj_ 開頭作為首碼

  • 該架構純ARC,相容的系統>=iOS6.0、iPhone\iPad橫豎屏
如果使用MJRefresh

cocoapods匯入:pod ‘MJRefresh‘ (cocoapods另一篇部落格裡面有寫)
手動匯入:
將MJRefresh檔案夾中的所有檔案拽入項目中
匯入主標頭檔:#import "MJRefresh.h"

#主要是實現上拉重新整理和下拉重新整理    //下拉重新整理    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopic)];    //自動更改透明度    self.tableView.mj_header.automaticallyChangeAlpha = YES;    //進入重新整理狀態    [self.tableView.mj_header beginRefreshing];    //上拉重新整理    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];    //結束頭部重新整理    [weakSelf.tableView.mj_header endRefreshing]; //結束尾部重新整理    [weakSelf.tableView.mj_footer endRefreshing];
MJRefresh類結構圖
mjv587.png
mj87v5.png具體使用預設的下拉重新整理 - 01
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{        // 進入重新整理狀態後會自動調用這個block    }];    //或    // 設定回調(一旦進入重新整理狀態,就調用target的action,也就是調用self的loadNewData方法)    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];    // 馬上進入重新整理狀態    [self.tableView.header beginRefreshing];

[


(下拉重新整理01-普通)下拉動畫重新整理 - 02
        // 重新整理控制項的狀態        typedef NS_ENUM(NSInteger, MJRefreshState) {        /** 普通閑置狀態 */        MJRefreshStateIdle = 1,        /** 鬆開就可以進行重新整理的狀態 */        MJRefreshStatePulling,        /** 正在重新整理中的狀態 */        MJRefreshStateRefreshing,        /** 即將重新整理的狀態 */        MJRefreshStateWillRefresh,        /** 所有資料載入完畢,沒有更多的資料了 */        MJRefreshStateNoMoreData    };    // 設定回調(一旦進入重新整理狀態,就調用target的action,也就是調用self的loadNewData方法)    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];    // 設定普通狀態的動畫圖片 (idleImages 是圖片)    [header setImages:idleImages forState:MJRefreshStateIdle];    // 設定即將重新整理狀態的動畫圖片(一鬆開就會重新整理的狀態)    [header setImages:pullingImages forState:MJRefreshStatePulling];    // 設定正在重新整理狀態的動畫圖片    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];    // 設定header    self.tableView.mj_header = header;

[


(下拉重新整理02-動畫圖片)下拉隱藏的屬性
// 隱藏時間header.lastUpdatedTimeLabel.hidden = YES;// 隱藏狀態header.stateLabel.hidden = YES;
自訂的文字和動畫圖片的設定
#使用上拉重新整理和下拉重新整理一般都是自訂頭部和尾部的類例如: 頭部重新整理的  @interface JHHeader : MJRefreshNormalHeader#在.m檔案中設定/** 初始化設定 */- (void)prepare{    [super prepare];    //建立UIImageView    UIImageView *logoView = [[UIImageView alloc] init];    //添加圖片    logoView.image = [UIImage imageNamed:@"meinv02"];    //將該UIImageView添加到當前header中    [self addSubview:logoView];    self.logoView = logoView;    //根據拖拽的情況自動切換透明度    self.automaticallyChangeAlpha = YES;    //隱藏時間    self.lastUpdatedTimeLabel.hidden = YES;    //設定文字顏色    self.stateLabel.textColor = [UIColor redColor];}/** *  擺放子控制項 */- (void)placeSubviews{    [super placeSubviews];    self.logoView.x = 0;    self.logoView.width = self.width;    self.logoView.height = 100;    self.logoView.y =  -self.logoView.height; }
自訂尾部重新整理
例:建立一個JHFooter類 繼承於MJRefreshAutoNormalFooter@interface JHFooter : MJRefreshAutoNormalFooter#在.m檔案中設定/**  初始化 */- (void)prepare{    [super prepare];    self.stateLabel.textColor = [UIColor grayColor];    [self setTitle:@"向上拉載入更多內容喔..." forState:MJRefreshStateIdle];    [self setTitle:@"正在串連網路載入中...." forState:MJRefreshStateRefreshing];    UISwitch *swith = [[UISwitch alloc] init];    [self addSubview:swith];    UIImageView *logoView = [[UIImageView alloc] init];    logoView.image = [UIImage imageNamed:@"meinv02"];    [self addSubview:logoView];    self.logoView = logoView;}/** *  擺放子控制項 */- (void)placeSubviews{    [super placeSubviews];    self.logoView.x = 0;    self.logoView.y = self.height;    self.logoView.width = self.width;    self.logoView.height = 100;}

大部分參考自MJRefresh 的 README.md
該架構還有很多強大的功能,詳情可以進入最上面給的github中查看
給出的這些應該足夠平時的開發,希望多多給建議



文/會跳舞的獅子(簡書作者)
原文連結:http://www.jianshu.com/p/e1a40f46aed7
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

iOS重新整理第三方MJRefresh的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.