Objective-C:UIScrollView手动分页
我想使用滚动视图作为水平模式下的选择器。 滚动视图最多可容纳七个子视图。 每个子视图代表一个值。 始终可见三个视图,中间的视图是选定的视图。
滚动视图在开始时可见:
__ | V1 | V2
Scrollview 设置为视图/值二:
V1 | V2 | V3
滚动视图设置为最后一个值:
V2 | V3 | __
我遇到的真正问题是“pagingEnabled”标志。 如果 pagingEnabled 设置为 YES,则滚动视图页面始终包含三个子视图/值,而不是只有一个。 如果 pagingEnabled 设置为 NO,则滚动视图不会固定。
对于我的问题有一个好的解决方案吗?
多谢, 丹;)
I want to use the scrollview as something like a picker in horizontal mode.
The scrollview holds up to seven subviews.
Each subview represents a value.
Always three views are visible and the one in the middle is the selected one.
Scrollview visible at start:
__ | V1 | V2
Scrollview set to view/value two:
V1 | V2 | V3
Scrollview set to last value:
V2 | V3 | __
The real problem I have got is the "pagingEnabled" flag.
If pagingEnabled is set to YES the scrollview pages always three subviews/values instead of only one.
If pagingEnabled is set to NO the scrollview does not clinch.
Is there a nice solution for my problem?
Thanks a lot,
Dan ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将滚动视图的框架更改为仅显示中间视图(即其原始宽度的三分之一,并偏移相同的量),然后将其
clipsToBounds
属性设置为 NO。Change the frame of the scrollview to be as if it were only displaying the middle view (i.e. a third of its original width, and offset by the same amount), but then set its
clipsToBounds
property to NO.如果其他人感兴趣,我找到了解决方案。
为您分配滚动视图的委托。 OvveridescrollViewDidEndDecelerating,然后通过执行类似的操作来获取当前索引(您想要的页面)。
I found a solution if anyone else is interested.
Assign you view the delegate of a scrollview. Ovveride scrollViewDidEndDecelerating, afterwards get your current index(page you want) by doing something like.
从 iOS 5 开始,就有
scrollViewWillEndDragging:withVelocity:targetContentOffset:
UIScrollViewDelegate
上的委托方法。这允许您实现任意分页。为此,您首先需要将
pagingEnabled
属性设置为NO
,否则不会调用我正在讨论的委托方法。现在,只要用户抬起手指并且滚动视图想要确定在哪里完成滚动,滚动视图就会调用此委托方法。神奇之处在于最后一个参数,
targetContentOffset
:它是一个指向CGPoint
的指针,并用作输入/输出变量。这意味着这个变量告诉你滚动视图想要滚动到哪里。但它允许您修改这个目标位置。velocity
可能也很有趣,它可以指示用户是否“推动”滚动视图或移动它、停止,然后抬起手指。例如,下面的实现将目标
x
位置四舍五入到最接近的 100 倍数,从而使“页面”宽度为 100 点。Since iOS 5, there's the
scrollViewWillEndDragging:withVelocity:targetContentOffset:
delegate method onUIScrollViewDelegate
. This allows you to implement arbitrary paging.For this to work, you first need to set the
pagingEnabled
property toNO
, otherwise the delegate method I'm talking about isn't called. The scroll view now calls this delegate method whenever the user lifts his finger and the scroll view wants to determine where to finish the scrolling.The magic is the last argument,
targetContentOffset
: it's a pointer to aCGPoint
and used as a in/out variable. This means this variable tells you where the scrollview wants to scroll to. But it allows you modify this target location. Thevelocity
might also be of interest, it can give you an indication whether the user "pushed" the scroll view or moved it, stopped, then lifted his finger.For example, here's an implementation that rounds the target
x
location to the nearest multiple of 100, thus making "pages" of 100 points width.