UITableView上的Parralax jutter

时间:2015-11-21 16:23:55

标签: ios objective-c uitableview

我有几个UITableView带有我应用视差效果的自定义单元格。在我的Tableview中,我打电话给

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *visibleCells = [self.tableView visibleCells];

    for (FeedTableViewCell *cell in visibleCells) {
        [cell cellOnTableView:self.tableView didScrollOnView:self.view];
    }
}

在我的自定义单元格中,方法如下所示:

- (void)cellOnTableView:(UITableView *)tableView didScrollOnView:(UIView *)view {
CGRect rectInSuperview = [tableView convertRect:self.frame toView:view];

float distanceFromCenter = CGRectGetHeight(view.frame)/2 - CGRectGetMinY(rectInSuperview);
float difference = CGRectGetHeight(self.image.frame) - CGRectGetHeight(self.frame);
float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;

CGRect imageRect = self.image.frame;
imageRect.origin.y = -(difference/2)+move;
self.image.frame = imageRect;
}

当我第一次加载tableview并滚动时,当图像更新时会有一个明显的“跳跃”,但是在这个初始设置之后,效果会很平滑。我正试图弄清楚如何摆脱那种跳跃。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

当你在那里开始滚动时,你需要在加载其他地方设置图像的视差帧,因为图像从默认值变为视差。为此,您可以添加。

添加:

[cell cellOnTableView:tableView didScrollOnView:self.view];

执行:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

如果您正在使用自动布局,您可能还需要:

[self.tableview reloadData];

to viewDidAppear