2009-11-29 23 views
10

我需要爲我的應用程序使用UIPagecontrol,並且我不確定如何開始。我是一個初學者,蘋果給我的例子非常複雜。我需要的僅僅是3頁,每頁都有不同的看法。有人可以幫我從這裏出去嗎?如何使用UIPageControl創建多個視圖?

P.S.記住我是初學者!

回答

3

這是一個不錯的example從可可與愛。它僅重用2個實際視圖,以獲得較低的內存佔用量。

+0

我很抱歉downvote,現在鏈接再次工作。 – hariseldon78 2012-11-05 00:18:54

22

你會想要使用UIScrollView,然後作爲兄弟,將UIPageControl定位在它上面。然後將每個頁面放入滾動視圖中,併爲其啓用分頁。通過這種方式,每一次「輕彈」都會將滾動視圖移動一頁。

現在,將您的視圖控制器分配給滾動視圖的委託,並監視scrollViewDidEndScrollAnimation,並使用contentOffset確定哪個頁面是最新的。

+4

哇,這似乎有點複雜。你有一個教程來做到這一點或示例代碼,所以我可以看到它是怎麼樣的? – lab12 2009-11-29 18:53:38

1

見我的答案在這裏:Is UIPageControl Useless By Itself?

一個可重用的類封裝UIPageControl和UIScrollView的。

+0

嗨,看起來你在創建這個課程方面做得很好。我是一個初學者,我不知道如何去告訴它要包含哪些UIviews(我已經將它們放在IB中)。 – Hippocrates 2011-03-03 22:38:14

+0

您必須以與您相同的方式實現PagedViewDelegate將實現一個UITableViewDelegate。 cellForRowAtIndexPath方法現在被viewForPageAtIndex所取代。您可以使用switch語句實現它,並使用[[NSBundle mainBundle] loadNibNamed: owner:nil]從相應的nib加載視圖。 – 2011-03-06 20:25:25

15

基於本戈特利布在那裏非常出色的正確答案,我得到了我想要的。下面是我用來實現它的代碼:

.H

@interface MyViewController : UIViewController <UIScrollViewDelegate> { 
} 
@property (nonatomic, retain) UIPageControl *helpPageCon; 

.M

@synthesize helpPageCon; 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    CGRect frame = [[UIScreen mainScreen] applicationFrame]; 
    float roundedValue = round(scrollView.contentOffset.x/frame.size.width); 
    self.helpPageCon.currentPage = roundedValue;  
} 

- (void)viewDidLoad 
{ 
    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-50)] autorelease]; 
    scrollView.contentSize = CGSizeMake(frame.size.width*5, frame.size.height-50); 
    [scrollView setPagingEnabled:YES]; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.delegate = self; 
    [self.view addSubview:scrollView]; 

    CGRect frame = [[UIScreen mainScreen] applicationFrame]; 
    self.helpPageCon = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)] autorelease]; 
    self.helpPageCon.center = CGPointMake(frame.size.width/2, frame.size.height-75); 
    self.helpPageCon.numberOfPages = 5; 
    [self.view addSubview:self.helpPageCon]; 
} 
- (void)dealloc 
{ 
    [super dealloc]; 
    [helpPageCon release]; 
} 
3

只需添加3次滾動視圖滾動視圖中啓用其他分頁後平鋪並在其中添加在scrollView下的一個UIPageControl控件,並且通過scrollview的兩個委託方法和pageview的Action方法的幫助,你可以實現UIPageControl的基本工作

我使用contentoffset找到當前頁面

// scrollview delegate 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 

    CGFloat pageWidth = mainScroll.frame.size.width; 
    int page = floor((mainScroll.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage=page; 

    } 

    // page view action method 

    - (IBAction)pageControlInteracted:(UIPageControl *)sender { 
     NSLog(@"%d",sender.currentPage); 
     CGRect frame = mainScroll.frame; 
     frame.origin.x = frame.size.width * sender.currentPage; 
     frame.origin.y = 0; 
     [mainScroll scrollRectToVisible:frame animated:YES]; 

    }