2011-04-18 66 views
6

是否有建議的方式來創建頁面比UISrollView的邊界更寬的分頁UIScrollView?如何用「超大」頁面創建分頁UIScrollView

我需要這樣的東西。內第2頁和尋呼模式

enter image description here

正常滾動與在頁面上的邊緣上的「橡皮」的效果。

對於我來說,分頁效果看起來有點複雜,如果你快速地輕彈你轉到下一頁,如果你慢地滑動,你會在邊緣看到新的頁面,並且只有在某個點後頁面被改變。

也許有人可以闡明處理這個問題的方法,這是甚至可能的唯一使用UIScrollViewDelegate方法或我必須繼承?

回答

2

我留下了深刻的印象。這實際上比我在一開始想的要容易得多。

簡單的解決方案是將每個頁面封裝在非分頁滾動視圖中。並做了。無需實現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>> 
0

據我所知,沒有辦法通過使用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座標移動的方向是大於或小於如果觸摸在屏幕上移動了足夠的數量,則使用scrollRectToVisible:以指定的分頁尺寸滾動視圖。