2011-10-08 45 views
0

我有一個在兩個視圖之間切換的滾動視圖。這兩個視圖在viewDidLoad中設置。我希望有一個頁面控件在選擇相應視圖時進行切換。簡單頁面控件

我看過的所有教程都很複雜。

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    CGRect pageFrame = CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.bounds.size.height); 
    scrollView.contentSize = CGSizeMake(pageFrame.size.width * 2, pageFrame.size.height); 
    scrollView.pagingEnabled = YES; 

    view1.frame = pageFrame; 
    [scrollView addSubview:view1]; 


    pageFrame.origin.x += pageFrame.size.width; 
    view2.frame = pageFrame; 
    [scrollView addSubview:view2]; 
} 

- (IBAction)changePage:(id)sender { 

} 

回答

1

當用戶點擊一個頁面控制,以移動到下一個或前一頁,控制發送由委託處理UIControlEventValueChanged事件。委託可以評估currentPage屬性以確定要顯示的頁面。頁面控件只在任一方向上前進一頁。

將ValueChanged操作的頁面控件的委託設置爲控制器的changePage方法。

- (IBAction)changePage:(id)sender { 
    UIPageControl *pageControl = (UIPageControl *)sender; 
    NSInteger currentPage = pageControl.currentPage; 
    CGPoint offset = CGPointMake(currentPage * scrollView.frame.size.width, 0); 
    [scrollView setContentOffset:offset animated:YES]; 
} 
+0

這工作完美!非常感謝。 – Vikings

0

設置一個int爲您UIButton S的tag屬性以跟蹤按下其中一個。

-(IBAction)changePage:(id)sender { 
    UIButton *button = (UIButton *) sender; 
    CGPoint offset = view1.frame.origin; 
    if (button.tag == kConstantForSecondPage) { 
     offset = view2.frame.origin; 
    } 
    [scrollView setContentOffset:offset animated:YES]; 
} 
+0

我並不真正理解這個概念。我在滾動視圖中有兩個視圖。按鈕的目的是什麼? – Vikings

+0

你想要一個IBAction - 所以我給你寫了一個。你不想要一個按鈕嗎? - 無論如何,代碼會告訴你如何滾動。 – Mundi

+0

那麼我有一個滾動視圖,加載和兩個視圖工作,我只是想要一個頁面控制切換,以及如何做到這一點。 – Vikings