单元格背景视差,如在 iOS7 天气应用程序中



如果你看一下iPhone上的iOS 7天气应用程序,你会看到当你滚动时,UITableViewCells的背景也会滚动(每个都独立于其他)。我试图弄清楚它是如何完成的。有什么想法吗?


答案是

-(void)scrollTable:(UIScrollView *)scrollView
{
    float offset = _tableViewNew.contentOffset.y / _tableViewNew.frame.size.height;
    for (int i = 0; i <[cellTitle count]; i++) {
        UITableViewCell *cell = [_tableViewNew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        CGRect frame = CGRectMake(cell.backgroundView.frame.origin.x, offset * 50, cell.backgroundView.frame.size.width, cell.backgroundView.frame.size.height);
        cell.backgroundView.frame = frame;
    }
}

我不会称之为视差效应。它所做的只是设置所有单元格背景相对于滚动位置的起始位置。假设您的单元格背景图像高 150 个单位,总可滚动高度为 400 个单位。

滚动百分比为:

relative scroll offset = tableView.contentOffset.Y / 400.

每当表格视图滚动时(您可以通过实现 UIScrollViewDelegate's scrollViewDidScroll:) 来调整单元格背景的垂直位置:

cell background offset = relative scroll offset * 150

为了实现scrollViewDidScroll:您需要设置 UITableView's 属性委托。正如UITableView子类UIScrollView,这个属性需要一个UIScrollViewDelegate实例。在控制器中,实现scrollViewDidScroll:并将委托属性设置为控制器。

最新更新