在我的应用程序中,我使用刷新控件和集合视图。
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
实例分配给UITableView
的私有财产_refreshControl
来轻松解决:
@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,此更改导致跳转。
覆盖setContentInset
上的UICollectionView
并考虑到这种情况似乎有助于:
- (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];
});
}