国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發教程之UIRefreshControl使用的踩坑指南

2019-10-21 18:41:19
字體:
來源:轉載
供稿:網友

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];}// 設置刷新狀態- (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]; }}// 設置刷新狀態- (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,//這個就不能下拉滑動,沒法實現下拉;但是設置下面這個就可以實現下拉了self.rootView.collectionView.alwaysBounceVertical = YES;

問題描述

接上一個話題,實現了TabBar的點擊刷新以后,開始繼續寫完成功能,刷新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{//添加一條數據[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接收會有問題 數字不對//因為上個界面傳過來的時候封裝成了對象,所以用NSNumber接收后再取值NSNumber *index = notification.object;if ([index intValue] == 1) {//刷新[self.newsTableView.refreshControl beginRefreshing];}}

此時的效果如下,直接下拉刷新可以,但是點擊TabBar不可以:

ios,uirefreshcontrol,refreshcontrol

刷新異常情況.gif

分析問題

經過Google幫助,終于知道原因,因為系統自帶的UIRefreshControl有兩個陷阱:

  • 調用-beginRefreshing方法不會觸發UIControlEventValueChanged事件;
  • 調用-beginRefreshing方法不會自動顯示進度圈。

也就是說,只是調用-beginRefreshing方法是不管用的,那么對應的需要做兩件事:

  • 手動設置UIRefreshControl的事件;
  • 手動設置UITableView的ContentOffset,露出進度圈。

解決問題

只需要修改上面第3步中的代碼如下:

-(void)doubleClickTab:(NSNotification *)notification{//這里有個坑 就是直接用NSInteger接收會有問題 數字不對//因為上個界面傳過來的時候封裝成了對象,所以用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,refreshcontrol
刷新正常情況.gif

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林周县| 区。| 平舆县| 博客| 桐梓县| 吐鲁番市| 宁远县| 筠连县| 仪征市| 乌审旗| 安多县| 松江区| 尼木县| 晋州市| 云梦县| 金昌市| 杭锦后旗| 贺州市| 乐山市| 榆林市| 清原| 镶黄旗| 宣威市| 甘谷县| 常山县| 灵寿县| 应城市| 海兴县| 万源市| 磴口县| 临邑县| 五常市| 梅州市| 东海县| 江油市| 通渭县| 瓮安县| 天柱县| 西林县| 定陶县| 石嘴山市|