所以我不想改變child view controllers
的任何代碼,但我敢肯定沒有任何方法讓父級直接捕獲子滾動,除非我將其設置爲child tableviews
的delegate
,但我不能這樣做,因爲每個child view controller
都需要成爲他們自己的tableview委託。
糾正我,如果我失去了一些東西。
我所做的就是從每個孩子的scrollViewDidScroll
方法來發送通知,如下所示:
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSDictionary *scrollViewInfo = [NSDictionary dictionaryWithObject:scrollView forKey:@"someKey"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"someNotificationName" object:nil userInfo:scrollViewInfo];
}
,然後簡單地觀察到,在父:
#pragma mark - Notifications
- (void)registerForNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(childScrollViewDidScroll:) name:@"someNotificationName" object:nil];
}
- (void)childScrollViewDidScroll:(NSNotification *)note {
UIScrollView* scrollView = [[note userInfo] valueForKey:@"someKey"];
CGFloat scrollOffset = scrollView.contentOffset.y;
CGRect headerFrame = self.headerView.frame;
headerFrame.origin.y = -scrollOffset;
self.headerView.frame = headerFrame;
}
它的滾動設備上的標題視圖沒有滯後,所以至少目前我很滿意。
我不認爲這會表現得那麼細膩。當用戶快速滾動時,不能期望通知被視爲像滾動一樣順暢。 –