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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容