刷新是我們經(jīng)常用到的功能肯腕,國內(nèi)MJ的刷新三方庫也是大多是app所采用的。不過新手在實(shí)現(xiàn)這一功能的時(shí)候都是直接拿過來使用而沒有思考怎么實(shí)現(xiàn)這一功能姊途,正好手上的項(xiàng)目差多了知态,自己抽個(gè)時(shí)候?qū)懸幌滤⑿聦?shí)現(xiàn)的過程,自己也順便復(fù)習(xí)一下贡茅。嘿嘿嘿其做。
內(nèi)置刷新
其實(shí)蘋果對(duì)于tabeView,有內(nèi)置的刷新控件驹沿。因?yàn)槭枪俜降腶pi 實(shí)現(xiàn)起來特別的簡單蹈胡,效果也不錯(cuò)罚渐。
//下拉刷新
UIRefreshControl *refreshC = [[UIRefreshControl alloc] init];
refreshC.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在刷新中"];
refreshC.tintColor = [UIColor blueColor];
[refreshC addTarget:self action:@selector(pullRefresh) forControlEvents:UIControlEventValueChanged];
_refreshC = refreshC;
self.refreshControl = refreshC;
這邊有一點(diǎn)要注意的是因?yàn)閁IRefreshControl繼承了UIControl 而且在h文件的注釋中有這樣的注釋。
When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event.
通過這兩個(gè)條件就可以觸發(fā)刷新事件的處理過程了合砂。
在刷新 獲取數(shù)據(jù)的過程中源织,采用異步加載。
- (void) pullRefresh {
//異步加載數(shù)據(jù)
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//加載數(shù)據(jù)
//加載完成之后停止刷新
//這里延時(shí)模擬
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_refreshC endRefreshing];
});
});
}
蘋果提供的這個(gè)刷新控制類UIRefreshControl
實(shí)現(xiàn)起來簡單便捷幻工,不過也有部分的局限囊颅。蘋果只提供了對(duì)顏色和文字的接口傅瞻,如果自己想實(shí)現(xiàn)帥氣的動(dòng)畫那就需要自己自動(dòng)已刷新控件了。
自定義刷新
有時(shí)候不得不自定義刷新效果,因?yàn)橄到y(tǒng)只提供了下拉刷新溺森,而上拉刷新必須要我們自己去實(shí)現(xiàn)窑眯。
其實(shí)自定義刷新效果也不難了磅甩。模仿系統(tǒng)的刷新效果姥卢,完全可以自己做一個(gè)。
我就說下我的思路 貼下代碼僧叉。純?nèi)腴T級(jí)別的棺榔,沒有難點(diǎn)掷豺。
1 首先聲明下拉刷新需要的控件(我是由一個(gè)一個(gè)label 和一個(gè) indicator組成)
設(shè)置一些ui(位置都是寫死的,童鞋恩不要太在意細(xì)節(jié))
@property (nonatomic, strong) UIView *bottomBackView;
@property (nonatomic, strong) UILabel *bottomLabel;
@property (nonatomic, strong) UIActivityIndicatorView *bottomIndicatorView;
- (void) setBottomRefreshUI {
//獲取 tableview contentSize 的大小
CGFloat BackViewY = self.tableView.contentSize.height + CGRectGetMaxY(self.tableView.frame);
_bottomBackView = [[UIView alloc] initWithFrame:CGRectMake(0, BackViewY, CGRectGetWidth(self.tableView.frame), 80)];
_bottomIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 20, 20, 20)];
_bottomIndicatorView.color = [UIColor blackColor];
[_bottomBackView addSubview:_bottomIndicatorView];
_bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 200, 30)];
_bottomLabel.text = @"上拉刷新";
[self.bottomBackView addSubview:_bottomLabel];
// [self.view addSubview:_bottomBackView];
[self.view insertSubview:_bottomBackView atIndex:0];
}
這邊要注意的是 刷新視圖要放在最下面
2 之后需要監(jiān)聽tableview 的contentOffset(我這邊設(shè)置的是當(dāng)偏移量超過80的時(shí)候就觸發(fā)刷新效果)
這邊要注意的是 偏移量不是直接就是contentOffset 這邊要通過contentOffset和contentSize 以及屏幕的高度(這個(gè)是相對(duì)的)計(jì)算出來的题画。
//上拉刷新
[self setBottomRefreshUI];
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
#pragma mark kvo
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([keyPath isEqualToString:@"contentOffset"]) {
UITableView *tableView = (UITableView *)object;
CGPoint contentOffset = [[change valueForKey:NSKeyValueChangeNewKey] CGPointValue];
if (((contentOffset.y + screenHeight -tableView.contentSize.height) > 80)&&tableView.contentSize.height > 0) {
NSLog(@"%f===%f",contentOffset.x,contentOffset.y);
self.bottomBackView.center = CGPointMake(tableView.center.x, tableView.contentSize.height+40);
[self beginRefresh];
}
}
}
3 當(dāng)達(dá)到觸發(fā)事件
改變刷新視圖的ui 以及異步加載所需要的數(shù)據(jù)
- (void) beginRefresh {
self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 100, 0);
[_bottomIndicatorView startAnimating];
_bottomLabel.text = @"松開開始刷新";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.1 animations:^{
self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
}completion:^(BOOL finished) {
[_bottomIndicatorView stopAnimating];
_bottomLabel.text = @"上拉開始刷新";
}];
});
}
到這邊就over了,上面是我自己寫著玩的 竞思,代碼也沒有封裝钞护。大家不要介意难咕。嘿嘿···
當(dāng)然MJ的刷新我在這就不介紹了,太流行了余佃,大家直接到看看他的readme 就可以了。椭懊。步势。