此项目的目标是当表格滚动时,在 UITableViewCell 上的 UIImageView 上创建视差效果。
此代码最初是为了在 meets 的发现标签上提供视差效果而创建的。
下载应用程序并免费试用!欢迎反馈!下载应用程序
在您的 UITableViewCell
上
parallaxImageView
放入具有 clipsToBounds = YES;
的 UIView
中,并设置一些偏移量。UITableView
滚动时的图像偏移量。- (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.parallaxImage.frame) - CGRectGetHeight(self.frame);
float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;
CGRect imageRect = self.parallaxImage.frame;
imageRect.origin.y = -(difference/2)+move;
self.parallaxImage.frame = imageRect;
}
在您的 UITableViewDelegate
中添加此函数。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Get visible cells on table view.
NSArray *visibleCells = [self.tableView visibleCells];
for (JBParallaxCell *cell in visibleCells) {
[cell cellOnTableView:self.tableView didScrollOnView:self.view];
}
}
就是这样,您可以通过演示项目来查看一切是如何协同工作的。