十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
這篇文章主要介紹iOS中UIRefreshControl的使用案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)是專業(yè)的宜昌網站建設公司,宜昌接單;提供成都網站建設、做網站,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行宜昌網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!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]; } // 設置刷新狀態(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]; } } // 設置刷新狀態(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:@"松開刷新"]; } } // 結束刷新 - (void) endRefreshControl { [_refreshControl endRefreshing]; } // 刷新的回調方法 - (void) loadData { [self endRefreshControl]; // [self performSelector:@selector(endRefreshControl) withObject:nil afterDelay:2]; } //設置如果collection的內容沒有占滿整個collectionView, //這個就不能下拉滑動,沒法實現(xiàn)下拉;但是設置下面這個就可以實現(xiàn)下拉了 self.rootView.collectionView.alwaysBounceVertical = YES;
問題描述
接上一個話題,實現(xiàn)了TabBar的點擊刷新以后,開始繼續(xù)寫完成功能,刷新UITableView,于是考慮到iOS 10以后,UIScrollView已經有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點擊事件
-(void)doubleClickTab:(NSNotification *)notification{ //這里有個坑 就是直接用NSInteger接收會有問題 數(shù)字不對 //因為上個界面?zhèn)鬟^來的時候封裝成了對象,所以用NSNumber接收后再取值 NSNumber *index = notification.object; if ([index intValue] == 1) { //刷新 [self.newsTableView.refreshControl beginRefreshing]; } }
此時的效果如下,直接下拉刷新可以,但是點擊TabBar不可以:
刷新異常情況.gif
分析問題
經過Google幫助,終于知道原因,因為系統(tǒng)自帶的UIRefreshControl有兩個陷阱:
調用-beginRefreshing方法不會觸發(fā)UIControlEventValueChanged事件;
調用-beginRefreshing方法不會自動顯示進度圈。
也就是說,只是調用-beginRefreshing方法是不管用的,那么對應的需要做兩件事:
手動設置UIRefreshControl的事件;
手動設置UITableView的ContentOffset,露出進度圈。
解決問題
只需要修改上面第3步中的代碼如下:
-(void)doubleClickTab:(NSNotification *)notification{ //這里有個坑 就是直接用NSInteger接收會有問題 數(shù)字不對 //因為上個界面?zhèn)鬟^來的時候封裝成了對象,所以用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]; } }
最終效果:
以上是“iOS中UIRefreshControl的使用案例”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)網站建設公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。