iOS開發(fā)教程之UIRefreshControl使用的踩坑指南
iOS UIRefreshControl基本用法
- (void) loadRefreshView
{
// 下拉刷新
_refreshControl = [[UIRefreshControl alloc] init];
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[_refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
[self.securityCollectionView addSubview:_refreshControl];
[self.securityCollectionView sendSubviewToBack:_refreshControl];
}
// 設(shè)置刷新狀態(tài)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
decelerate = YES;
if (scrollView.contentOffset.y < HEIGHT_REFRESH) {
dispatch_async(dispatch_get_main_queue(), ^{
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在刷新"];
});
[_refreshControl beginRefreshing];
[self loadData];
}
}
// 設(shè)置刷新狀態(tài)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y >= HEIGHT_REFRESH) {
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
}
else if (!scrollView.decelerating) {
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"松開刷新"];
}
}
// 結(jié)束刷新
- (void) endRefreshControl
{
[_refreshControl endRefreshing];
}
// 刷新的回調(diào)方法
- (void) loadData
{
[self endRefreshControl];
// [self performSelector:@selector(endRefreshControl) withObject:nil afterDelay:2];
}
//設(shè)置如果collection的內(nèi)容沒有占滿整個(gè)collectionView,
//這個(gè)就不能下拉滑動(dòng),沒法實(shí)現(xiàn)下拉;但是設(shè)置下面這個(gè)就可以實(shí)現(xiàn)下拉了
self.rootView.collectionView.alwaysBounceVertical = YES;
問題描述
接上一個(gè)話題,實(shí)現(xiàn)了TabBar的點(diǎn)擊刷新以后,開始繼續(xù)寫完成功能,刷新UITableView,于是考慮到iOS 10以后,UIScrollView已經(jīng)有UIRefreshControl的屬性了,干脆用自帶的寫。于是就有了如下的代碼:
添加UIRefreshControl到UITableView上去
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; refreshControl.tintColor = [UIColor grayColor]; refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"]; [refreshControl addTarget:self action:@selector(refreshTabView) forControlEvents:UIControlEventValueChanged]; self.newsTableView.refreshControl = refreshControl;
下拉刷新事件
-(void)refreshTabView
{
//添加一條數(shù)據(jù)
[self.newsData insertObject:[self.newsData firstObject] atIndex:0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.newsTableView reloadData];
if ([self.newsTableView.refreshControl isRefreshing]) {
[self.newsTableView.refreshControl endRefreshing];
}
});
}
TabBar點(diǎn)擊事件
-(void)doubleClickTab:(NSNotification *)notification{
//這里有個(gè)坑 就是直接用NSInteger接收會有問題 數(shù)字不對
//因?yàn)樯蟼€(gè)界面?zhèn)鬟^來的時(shí)候封裝成了對象,所以用NSNumber接收后再取值
NSNumber *index = notification.object;
if ([index intValue] == 1) {
//刷新
[self.newsTableView.refreshControl beginRefreshing];
}
}
此時(shí)的效果如下,直接下拉刷新可以,但是點(diǎn)擊TabBar不可以:

刷新異常情況.gif
分析問題
經(jīng)過Google幫助,終于知道原因,因?yàn)橄到y(tǒng)自帶的UIRefreshControl有兩個(gè)陷阱:
- 調(diào)用-beginRefreshing方法不會觸發(fā)UIControlEventValueChanged事件;
- 調(diào)用-beginRefreshing方法不會自動(dòng)顯示進(jìn)度圈。
也就是說,只是調(diào)用-beginRefreshing方法是不管用的,那么對應(yīng)的需要做兩件事:
- 手動(dòng)設(shè)置UIRefreshControl的事件;
- 手動(dòng)設(shè)置UITableView的ContentOffset,露出進(jìn)度圈。
解決問題
只需要修改上面第3步中的代碼如下:
-(void)doubleClickTab:(NSNotification *)notification{
//這里有個(gè)坑 就是直接用NSInteger接收會有問題 數(shù)字不對
//因?yàn)樯蟼€(gè)界面?zhèn)鬟^來的時(shí)候封裝成了對象,所以用NSNumber接收后再取值
NSNumber *index = notification.object;
if ([index intValue] == 1) {
//刷新
//animated不要為YES,否則菊花會卡死
[self.newsTableView setContentOffset:CGPointMake(0, self.newsTableView.contentOffset.y - self.newsTableView.refreshControl.frame.size.height) animated:NO];
[self.newsTableView.refreshControl beginRefreshing];
[self.newsTableView.refreshControl sendActionsForControlEvents:UIControlEventValueChanged];
}
}
最終效果:

刷新正常情況.gif
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS對數(shù)組進(jìn)行排序的實(shí)例代碼
本文通過實(shí)例代碼給大家講解了ios對數(shù)組進(jìn)行排序的實(shí)例方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-08-08
以代碼實(shí)例總結(jié)iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲方式
這篇文章主要介紹了iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲方式的實(shí)例總結(jié),代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-02-02
iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南
這里我們要介紹的是AVAudio ToolBox框架中的AudioServicesPlaySystemSound函數(shù)創(chuàng)建的服務(wù),特別適合用來制作鈴聲,下面就簡單整理一下iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南:2016-06-06
iOS App開發(fā)中的UIStackView堆疊視圖使用教程
UIStackView是iOS9以來新增加的組件,使我們能夠?qū)IView子類對象進(jìn)行靈活排版,這里我們就來看一下iOS App開發(fā)中的UIStackView堆疊視圖使用教程2016-07-07
iOS實(shí)現(xiàn)對不同分辨率設(shè)備的字號大小適配方法
下面小編就為大家分享一篇iOS實(shí)現(xiàn)對不同分辨率設(shè)備的字號大小適配方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

