我想为具有全景图像的UI ImageView(它是滚动视图的子视图)实现无限滚动.当用户通过手动滚动到达最后时,应该再次开始相同的图像. 示例全景图像: 我最终创建了一个scrollview,里面有一个
示例全景图像:
我最终创建了一个scrollview,里面有一个水平stackview. stackview将动态添加和删除视图,因为用户将滚动到结尾.诀窍是在添加或删除视图后重置contentOffset,以便用户体验无缝的无限滚动.
func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.x <= 0 { addPanoramaViewFromStackView(atPosition: 0) removePanoramaViewFromStackView(atPosition: 1) scrollView.contentOffset.x = UIScreen.main.bounds.height * ratio } else if (scrollView.contentOffset.x >= (scrollView.contentSize.width - scrollView.frame.size.width)) { addPanoramaViewFromStackView(atPosition: 2) removePanoramaViewFromStackView(atPosition: 0) scrollView.contentOffset.x = scrollView.contentOffset.x - UIScreen.main.bounds.height * ratio } }
ratio是全景图像的宽度和高度之比.由于图像占据设备的整个高度,因此宽度将按比例计算.