如何创建具有“超大”尺寸的分页 UIScrollView页面

发布于 2024-11-02 11:51:58 字数 332 浏览 10 评论 0原文

是否有建议的方法来创建页面比 UISrollView 边界更宽的分页 UIScrollView?

我需要这样的东西。

在此处输入图像描述

在 page2 和分页模式下正常滚动,页面边缘有“橡皮筋”效果。

分页效果对我来说看起来有点复杂,如果你快速滑动,你会进入下一页,如果你滑动缓慢,你会在边缘看到新页面,只有在某个点之后页面才会更改。

也许有人可以阐明处理此问题的方法,仅使用 UIScrollViewDelegate 方法是否可以实现这一点,或者我是否必须子类化?

Is there a suggested way to create a paging UIScrollView that has pages wider than the bounds of the UISrollView?

I would need something like this.

enter image description here

normal scrolling within page2 and paging mode with the "rubberband" effect on the edges of the pages.

The paging effect looks a bit complicated for me, if you flick fast you go to the next page, if you slide slow you see the new page at the edge and only after a certain point the page is changed.

Maybe somebody can shed some light on the way to handle this, is this even possible with the sole use of UIScrollViewDelegate methods or do I have to subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

近箐 2024-11-09 11:51:58

我印象深刻。这实际上比我一开始想象的要容易得多。

简单的解决方案是将每个页面封装在非分页滚动视图中。完成了。无需实现 UIScrollViewDelegate,无需子类化。三行额外的代码

对于常规大小的页面,我有这样的东西:

UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[mainScroller addSubview:myCustomView];
totalWidth += width;

现在我有这样的:

UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, bigWidth, height)] autorelease];
UIScrollView *secondaryScroller = [[[UIScrollView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[secondaryScroller setContentSize:myCustomView.frame.size];
[secondaryScroller addSubview:myCustomView];
[mainScroller addSubview:secondaryScroller];
totalWidth += width;

三行。惊人的。


视图层次结构:

<UIScrollView: 0x4b32eb0; frame = (0 0; 768 1004); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x4b32d00>; contentOffset: {0, 0}>
   | <UIScrollView: 0x4b32710; frame = (0 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b35580>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b33f70; frame = (0 0; 1352 1004); layer = <CALayer: 0x4b16c20>>
   | <UIScrollView: 0x4b34790; frame = (768 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33e10>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b30fa0; frame = (0 0; 789 1004); layer = <CALayer: 0x4b329f0>>
   | <UIScrollView: 0x4b34920; frame = (1536 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33180>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b30d00; frame = (0 0; 1398 1004); layer = <CALayer: 0x4b33120>>
   | <UIScrollView: 0x4b31fe0; frame = (2304 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b32170>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b34c50; frame = (0 0; 863 1004); layer = <CALayer: 0x4b31f80>>
   | <UIScrollView: 0x4b32460; frame = (3072 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b325f0>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b323d0; frame = (0 0; 1064 1004); layer = <CALayer: 0x4b32400>>

I'm impressed. This was actually much much easier than I thought in the beginning.

The simple solution was to encapsulate each page in a non-paging scrollview. And done. No need to implement UIScrollViewDelegate, no need to subclass. Three extra lines of code

For the regular sized pages I had something like this:

UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[mainScroller addSubview:myCustomView];
totalWidth += width;

and now I have this:

UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, bigWidth, height)] autorelease];
UIScrollView *secondaryScroller = [[[UIScrollView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[secondaryScroller setContentSize:myCustomView.frame.size];
[secondaryScroller addSubview:myCustomView];
[mainScroller addSubview:secondaryScroller];
totalWidth += width;

Three lines. Amazing.


The view hierarchy:

<UIScrollView: 0x4b32eb0; frame = (0 0; 768 1004); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x4b32d00>; contentOffset: {0, 0}>
   | <UIScrollView: 0x4b32710; frame = (0 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b35580>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b33f70; frame = (0 0; 1352 1004); layer = <CALayer: 0x4b16c20>>
   | <UIScrollView: 0x4b34790; frame = (768 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33e10>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b30fa0; frame = (0 0; 789 1004); layer = <CALayer: 0x4b329f0>>
   | <UIScrollView: 0x4b34920; frame = (1536 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33180>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b30d00; frame = (0 0; 1398 1004); layer = <CALayer: 0x4b33120>>
   | <UIScrollView: 0x4b31fe0; frame = (2304 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b32170>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b34c50; frame = (0 0; 863 1004); layer = <CALayer: 0x4b31f80>>
   | <UIScrollView: 0x4b32460; frame = (3072 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b325f0>; contentOffset: {0, 0}>
   |    | <UIView: 0x4b323d0; frame = (0 0; 1064 1004); layer = <CALayer: 0x4b32400>>
爱的十字路口 2024-11-09 11:51:58

我使用了本教程 -

http://www.edumobile .org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/

如果您想制作更大的页面,您可以在本教程中增加 PageControlExampleViewControl 的视图大小。假设将其宽度设置为 360 而不是默认的 320。

I used this tutorial -

http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/

if you want to make a larger page, you can increase the size of view of PageControlExampleViewControl in this tutorial. Lets say make its width to 360 instead of default 320.

节枝 2024-11-09 11:51:58

据我所知,没有办法直接使用scrollviews分页属性来实现这一点。

您必须实现自己的 UIScrollView 子类,并且在实现文件中您需要实现:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event; 

使用 contentOffset 属性计算出滚动视图滚动了多少。

并利用 UIScrollViews 的 scrollRectToVisible: 来实现您自己的自定义滚动功能。

[self scrollRectToVisible:CGRectMake(horizontalScrollAmount,virticalScrollAmount,rectWidth,rectHeight) animated:NO];

事件链类似于:记录开始触摸的位置,如果触摸移动,通过检查其 x/y 坐标是否大于或小于其起始位置来找出它移动的方向,如果touch 在屏幕上移动了足够的距离,然后使用 scrollRectToVisible: 按指定的分页大小滚动视图。

As far as I know, there is no way of achieving this directly by using the scrollviews paging property.

You would have to implement your own UIScrollView subclass and within your implementation file you would need to implement:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event; 

Work out how much the scroll view has scrolled using the contentOffset property.

And make use of UIScrollViews's scrollRectToVisible: to implement your own custom scrolling functionality.

[self scrollRectToVisible:CGRectMake(horizontalScrollAmount,virticalScrollAmount,rectWidth,rectHeight) animated:NO];

The chain of events would be something like: record the location of a beginning touch, if the touch moves, find out which direction it moved by checking to see if its x/y coordinate is greater than or less than its starting position, if the touched moved a sufficient amount across the screen, then scroll the view by your designated paging size using scrollRectToVisible:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文