[英]Multiple Scrollviews?
我真的不知道如何解釋我想要的東西,所以這是一張照片:
我有很多子視圖(灰色線)的視圖。 然后背景(藍色)是圖片(UIImageView +模糊效果),因此我需要將其保留下來而不要滾動。 在背景的后面,有一個視圖(橙色)。 我希望圖片(藍色)僅在子視圖(灰色)在底部(第三張圖片)時滾動。
我應該使用嵌入的滾動視圖,還是只能使用一個UIScrollView來獲得這種效果? 如果有多個滾動視圖,是否有人舉一個例子?
非常感謝你的幫助
僅使用一個滾動視圖,並使用其委托方法'scrollViewDidScroll'讀取內容偏移量。 使用此值可轉換背景圖像。
不必使用2個滾動視圖。 但是我會使用它,因為我更喜歡設置背景滾動視圖的contentOffset
而不是直接設置它的框架。
這個想法是為前滾動視圖實現scrollViewDidScroll:
委托方法。 然后通過檢查scrollView.contentOffset.y + scrollView.bounds.size.height > scrollView.contentSize.height
來跟蹤contentOffset
以檢查滾動是否已到達其內容的結尾。
如果具有,則使用超出內容大小的偏移量來偏移背景視圖的位置。
請參見下面的代碼。 您可以跳過所有代碼,而僅查看scrollViewDidScroll:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.backgroundScrollView.userInteractionEnabled = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [NSString stringWithFormat:@"Item %ld", (long)indexPath.row];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat backgroundVerticalOffset = MAX(scrollView.contentOffset.y + scrollView.bounds.size.height - scrollView.contentSize.height, 0);
CGPoint backgroundContentOffset = CGPointMake(0.0, backgroundVerticalOffset);
self.backgroundScrollView.contentOffset = backgroundContentOffset;
// If you decide not to use two scroll views, then please use backgroundContentOffset to set the backgroundView.frame.origin.y instead
// e.g. backgroundView.frame.origin.y = -backgroundContentOffset;
}
結果如下圖所示:
PS我使用藍色背景視圖而不是圖像視圖,其他所有背景顏色均清晰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.