我希望为iOS构建一个连续的视差滚动视图,它将充当一个tableView。我想要重新创建的内容非常类似于在此页上显示的滚动。我见过很多“双视差”视差视图,但没有一个是连续的。
我知道这不是一个直接的问题,但我确实需要一些帮助,使其朝着正确的方向发展。
发布于 2014-03-18 10:14:51
有很多方法可以解决这个问题。
我可能不会使用表视图,但我认为从第一眼看,我会使用一个UIScrollView
、2个UIImageViews
和一个图像的NSArray
(取决于有多少图像)。
两个图像视图将被放置在主view
的另一个之上,然后它们的顶部都将是滚动视图,其中没有任何内容。滚动视图仅用作控件。
根据“页面”的数量设置滚动视图的内容大小(即屏幕高度乘以图像数量)。
在滚动视图委托方法中,-(void)scrollViewDidScroll:
获取滚动视图的当前偏移量。
您可以使用此偏移量来计算topImage
和bottomImage
(即它们将来自NSArray
的哪个索引)。
然后根据偏移量再次更改顶部UIIMageView
的位置。
粗码
// create a topImageView and bottomImageView.
// set contentMode UIViewContentModeScaleAspectFill
// set clipsToBounds = YES
// place them both on self.view (bottom first)
// create a scrollView with a clear background.
// place it on the self.view.
// get the array of images.
// set the scrollView.contentSize = number of images * height of scrollView.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat percentageOffset = scrollView.contentOffset.y / CGRectGetHeight(scrollView.frame);
NSInteger topPage = floorf(percentageOffset);
CGFloat topPageOffset = percentageOffset - topPage;
// make sure that you're not going outside of the index bounds first!!!!
self.topImageView.image = self.images[topPage];
self.bottomImageView.image = self.images[topPage + 1];
// move the top image view so it looks like you are scrolling it.
self.topImageView.frame = CGRectOffset(self.view.bounds, -self.view.bounds.height * topPageOffset, 0);
}
这将给人的印象是,你是在滑动顶部的图像的方式,并揭示底部的形象。它也将持续工作,所以如果您“按下”滚动视图,滚动视图将一个接一个地显示,直到滚动视图缓慢并停止。
我想这就是我处理它的方法。
https://stackoverflow.com/questions/22487100
复制