[英]UIRefreshControl with UICollectionView in iOS7
在我的應用程序中,我使用刷新控件和集合視圖。
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
collectionView.alwaysBounceVertical = YES;
...
[self.view addSubview:collectionView];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[collectionView addSubview:refreshControl];
iOS7有一些令人討厭的錯誤,當你向下拉集合視圖並且在刷新開始時不釋放你的手指時,垂直contentOffset
向下移動20-30點,這導致丑陋的滾動跳躍。
如果在UITableViewController
之外使用刷新控件,表也有這個問題。 但是對於它們來說,可以通過將UIRefreshControl
實例分配給名為_refreshControl
UITableView
的私有屬性來輕松解決:
@interface UITableView ()
- (void)_setRefreshControl:(UIRefreshControl *)refreshControl;
@end
...
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:tableView];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[tableView addSubview:refreshControl];
[tableView _setRefreshControl:refreshControl];
但是UICollectionView
沒有這樣的屬性,因此必須有一些方法來手動處理它。
遇到同樣的問題,發現似乎可以修復它的解決方法。
這似乎正在發生,因為當您拉過滾動視圖的邊緣時, UIScrollView
正在減慢對平移手勢的跟蹤。 但是, UIScrollView
不會在跟蹤期間考慮對contentInset的更改。 UIRefreshControl
在激活時更改contentInset,此更改導致跳轉。
覆蓋UICollectionView
上的setContentInset
並解釋此案例似乎有助於:
- (void)setContentInset:(UIEdgeInsets)contentInset {
if (self.tracking) {
CGFloat diff = contentInset.top - self.contentInset.top;
CGPoint translation = [self.panGestureRecognizer translationInView:self];
translation.y -= diff * 3.0 / 2.0;
[self.panGestureRecognizer setTranslation:translation inView:self];
}
[super setContentInset:contentInset];
}
有趣的是, UITableView
通過不降低跟蹤速度來解決這個問題,直到您將PAST拉入刷新控件。 但是,我沒有看到這種行為暴露的方式。
- (void)viewDidLoad
{
[super viewDidLoad];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(scrollRefresh:) forControlEvents:UIControlEventValueChanged];
[self.collection insertSubview:self.refreshControl atIndex:0];
self.refreshControl.layer.zPosition = -1;
self.collection.alwaysBounceVertical = YES;
}
- (void)scrollRefresh:(UIRefreshControl *)refreshControl
{
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refresh now"];
// ... update datasource
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Updated %@", [NSDate date]]];
[self.refreshControl endRefreshing];
[self.collection reloadData];
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.