30

내 애플리케이션에서 컬렉션보기로 새로 고침 컨트롤을 사용합니다.UIRefreshControl with iOS7의 UICollectionView

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
collectionView.alwaysBounceVertical = YES; 
... 
[self.view addSubview:collectionView]; 

UIRefreshControl *refreshControl = [UIRefreshControl new]; 
[collectionView addSubview:refreshControl]; 

iOS7에이 성가신 버그가 당신이 아래로 콜렉션 뷰를 끌어와 추한 스크롤 점프에 어떤 결과 아래로 지적 20-30를 들어, 시작 수직 contentOffset 변화를 새로 고칠 때 손가락을 해제하지 않을 때.

테이블에 UITableViewController 외부의 새로 고침 컨트롤을 사용하면 테이블에이 문제가 발생합니다. 그러나 그들을 위해 쉽게 _refreshControl라고 UITableView의 사유 재산에 UIRefreshControl 인스턴스를 할당하여 해결할 수 : 수동으로 처리 할 수있는 방법이 있어야합니다 그래서

@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는 속성이 없습니다.

답변

48

동일한 문제가있어 해결 방법이있는 것으로 나타났습니다.

UIScrollView이 스크롤보기의 가장자리를 지나갈 때 팬 제스처의 추적 속도가 느려지므로 이러한 현상이 발생하는 것으로 보입니다. 그러나 UIScrollView은 추적하는 동안 contentInset에 대한 변경 사항을 고려하지 않습니다. UIRefreshControl이 활성화되면 contentInset이 변경되고이 변경으로 인해 점프가 발생합니다.

당신의 UICollectionViewsetContentInset 재정이 경우 회계 도움이 보인다 :

- (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 차지한다. 그러나이 동작이 노출되는 방식이 표시되지 않습니다.

+0

글쎄, 확실히 문제가 덜 분명합니다. 이 계산에 어떻게 도착했는지 말해 줄 수 있니? 그것은 꽤 자리에 표시되지 않습니다. 나는 3/2 대신 1.7 배를 곱하면 가장 좋은 결과를 얻을 수 있습니다. 그러나 1.7은 3/2보다 더 자의적이라고 느낀다. 필요한 정확한 조정을 찾아내는 것이 좋을 것입니다 ... –

+0

좀 더 실험 한 후 3/2 정도가 맞았습니다. 그러나 그 분수는 어디에서 오는가? –

+0

제스처 인식기의 변환 값과 contentInset 포인트를지나 스크롤 할 때 스크롤 뷰에서 이동 한 차이 사이의 비율을 보면 경험적으로 결정됩니다. 완벽하지는 않으며 iOS 6에서는 다른 가치라고 생각합니다. –

1
- (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]; 
    }); 

}