0
我见过很多关于创建视差效果的教程,但是其中大多数教程似乎都只有一个图像和一个UITableView或使用加速度计。我没有遇到一个教程,显示如何使用UIScrollView来完成它。滚动视图上的Parralax效果
我期待的效果可以在Yik Yak和Two Dots中看到。任何教程/ githubs,因为我找不到它们?
所以要根据滚动位置寻找要移动的对象。
我见过很多关于创建视差效果的教程,但是其中大多数教程似乎都只有一个图像和一个UITableView或使用加速度计。我没有遇到一个教程,显示如何使用UIScrollView来完成它。滚动视图上的Parralax效果
我期待的效果可以在Yik Yak和Two Dots中看到。任何教程/ githubs,因为我找不到它们?
所以要根据滚动位置寻找要移动的对象。
我在一个单一的背景图像的UITableView的实现视差,但你可能只是通过所有对象循环,请根据您的看法如何“深”是不同的因素。
这只是它是如何基本上完成了,但我敢肯定,你就可以轻松地我的代码转换到您的项目:
// of cause, you have to have your view controller as delegate for the scroll view
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect backgroundImageFrame = [_backgroundImageView frame];
backgroundImageFrame.origin.y -= (scrollView.contentOffset.y - _previousY)/2.25;
// play around with different factors here to meet your needs here ^^^^
CGFloat scaledImageHeight = _backgroundImageView.image.size.height * _backgroundImageView.scaledImageSize.height;
CGFloat relativeImageY = _backgroundImageView.superview.frame.size.height - backgroundImageFrame.origin.y;
if ((scaledImageHeight - relativeImageY + 49) >= 0) {
// this is just a bit extra padding ^^
_previousY = scrollView.contentOffset.y;
[_backgroundImageView setFrame:backgroundImageFrame];
}
}