带有“圆形"的 UIScrollView滚动

时间:2022-10-16
本文介绍了带有“圆形"的 UIScrollView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在我的 UIScrollView 中进行循环"滚动,但没有成功.

I am trying to make "Circular" scrolling in my UIScrollView, but unsuccessful.

我想做什么:如果 uiscrollview 到达结束,它应该移动开始如果 uiscrollview 在开始并向后移动,它应该移动到结束

What I want to do: if uiscrollview reaches end, it should move to start if uiscrollview at start and moving back, it should move to end

在我的情况下附加滚动视图不是好方法(其他方法应该得到页面ID")

Appending scrollview isn't good way in my situation (other methods should get "page id")

你有什么想法吗?

推荐答案

我已经实现了这个方法,但是它需要启用分页.假设您有五个元素 A、B、C、D 和 E.设置视图时,将最后一个元素添加到开头,将第一个元素添加到结尾,并调整内容偏移量以查看第一个元素,例如 E,[A],B,C,D,E,A.在 UIScrollViewDelegate 中,检查用户是否到达任何一端,并将偏移 without 动画移动到另一端.

I've implemented this method, but it requires paging enabled. Lets assume you have five elements A,B,C,D and E. When you set up your view, you add the last element to the beginning and the first element to the end, and adjust the content offset to view the first element, like this E,[A],B,C,D,E,A. In the UIScrollViewDelegate, check if the user reach any of the ends, and move the offset without animation to the other end.

想象一下 [ ] 表示正在显示的视图:

Imagine the [ ] indicates the view being shown:

E,A,B,C,[D],E,A

用户向右滑动

E,A,B,C,D,[E],A

用户向右滑动

E,A,B,C,D,E,[A]

然后,自动设置内容偏移到第二个元素

Then, automatically set the content offset to the second element

E,[A],B,C,D,E,A

通过这种方式,用户可以双向滑动创建无限滚动的错觉.

This way the user can swipe both ways creating the illusion of an infinite scroll.

E,A,[B],C,D,E,A


更新

我已经上传了该算法的完整实现.这是一个非常复杂的类,因为它还具有点击选择、无限循环滚动和单元格重用.您可以按原样使用代码,修改它或提取您需要的代码.最有趣的代码在 TCHorizontalSelectorView 类中.


Update

I've uploaded a complete implementation of this algorithm. It's a very complicated class, because it also has on-click selection, infinite circular scroll and cell reuse. You can use the code as is, modify it or extract the code that you need. The most interesting code is in the class TCHorizontalSelectorView.

文件链接

尽情享受吧!

UICollectionView 现在是实现此目的的推荐方法,它可用于获得完全相同的行为.本教程详细描述了如何去实现它.

UICollectionView is now the recommended way to achieve this and it can be used to obtain the very same behavior. This tutorial describes in details how to achieve it.

这篇关于带有“圆形"的 UIScrollView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!