Objective-C:UIScrollView手动分页

发布于 2024-08-29 16:10:03 字数 381 浏览 10 评论 0原文

我想使用滚动视图作为水平模式下的选择器。 滚动视图最多可容纳七个子视图。 每个子视图代表一个值。 始终可见三个视图,中间的视图是选定的视图。

滚动视图在开始时可见:

__ | 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城仅有旧梦在 2024-09-05 16:10:03

将滚动视图的框架更改为仅显示中间视图(即其原始宽度的三分之一,并偏移相同的量),然后将其 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.

邮友 2024-09-05 16:10:03

如果其他人感兴趣,我找到了解决方案。

为您分配滚动视图的委托。 OvveridescrollViewDidEndDecelerating,然后通过执行类似的操作来获取当前索引(您想要的页面)。

 NSNumber* currentIndex = [NSNumber numberWithInt:round(scrollview.Contentoffset.x / PAGE_SIZE)];

//Then just update your scrollviews offset with


[scrollview setContentOffset:CGPointMake([currentIndex intValue] * PAGE_SIZE, 0) animated:YES];

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.

 NSNumber* currentIndex = [NSNumber numberWithInt:round(scrollview.Contentoffset.x / PAGE_SIZE)];

//Then just update your scrollviews offset with


[scrollview setContentOffset:CGPointMake([currentIndex intValue] * PAGE_SIZE, 0) animated:YES];
硪扪都還晓 2024-09-05 16:10:03

从 iOS 5 开始,就有 scrollViewWillEndDragging:withVelocity:targetContentOffset: UIScrollViewDelegate 上的委托方法。这允许您实现任意分页。

为此,您首先需要将 pagingEnabled 属性设置为 NO,否则不会调用我正在讨论的委托方法。现在,只要用户抬起手指并且滚动视图想要确定在哪里完成滚动,滚动视图就会调用此委托方法。

神奇之处在于最后一个参数,targetContentOffset:它是一个指向 CGPoint 的指针,并用作输入/输出变量。这意味着这个变量告诉你滚动视图想要滚动到哪里。但它允许您修改这个目标位置。 velocity 可能也很有趣,它可以指示用户是否“推动”滚动视图或移动它、停止,然后抬起手指。

例如,下面的实现将目标 x 位置四舍五入到最接近的 100 倍数,从而使“页面”宽度为 100 点。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    targetContentOffset->x = round(targetContentOffset->x / 100.0) * 100.0;
}

Since iOS 5, there's the scrollViewWillEndDragging:withVelocity:targetContentOffset: delegate method on UIScrollViewDelegate. This allows you to implement arbitrary paging.

For this to work, you first need to set the pagingEnabled property to NO, 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 a CGPoint 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. The velocity 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.

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    targetContentOffset->x = round(targetContentOffset->x / 100.0) * 100.0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文