2012-01-29 20 views
3

我需要動態地在我的應用程序中創建一些視圖,並再次動態地放置一些按鈕。如果按鈕的數量(數量)多於十個,我希望將按鈕放在新視圖上,並且視圖之間的轉換必須與UIPageControl一起。即使我從Apple的開發者頁面搜索並搜索,我也找不到解決我的問題的方法。有人可以幫忙嗎?如何將視圖添加到UIScrollView並使其與UIPageControl保持同步?

+1

可能重複的[UIPageControl幫助](http://stackoverflow.com/questions/1816144/uipagecontrol-help) – 2012-01-29 22:56:25

+0

我不認爲這是重複我看過那個問題,但我不想用UIScrollView的。感謝您的警告 – 2012-01-29 23:23:23

回答

11

使用addSubview方法將您的視圖添加爲UIScrollView的並排子視圖。然後使用帶有UIScrollView的UIPageControl,如example


我發管理一個UIScrollView,一個UIPageControl,和UIViews的陣列的一類。它是我在自己的代碼中使用的簡化版本。它執行以下操作:

  • 設置滾動視圖以顯示UIView數組。它不關心視圖是否已動態生成。
  • 處理滾動和頁面控制事件。
  • 將滾動視圖與頁面控件同步。

PageViewManager.h

#import <Foundation/Foundation.h> 

@interface PageViewManager : NSObject <UIScrollViewDelegate> 
{ 
    UIScrollView* scrollView_; 
    UIPageControl* pageControl_; 
    NSArray* pages_; 
    BOOL pageControlUsed_; 
    NSInteger pageIndex_; 
} 

- (id)initWithScrollView:(UIScrollView*)scrollView 
      pageControl:(UIPageControl*)pageControl; 
- (void)loadPages:(NSArray*)pages; 
- (void)loadControllerViews:(NSArray*)pageControllers; 

@end 

PageViewManager.m

#import "PageViewManager.h" 

@interface PageViewManager() 

- (void)pageControlChanged; 

@end 

@implementation PageViewManager 

- (id)initWithScrollView:(UIScrollView*)scrollView 
      pageControl:(UIPageControl*)pageControl 
{ 
    self = [super init]; 
    if (self) 
    { 
     scrollView_ = scrollView; 
     pageControl_ = pageControl; 
     pageControlUsed_ = NO; 
     pageIndex_ = 0; 

     [pageControl_ addTarget:self action:@selector(pageControlChanged) 
       forControlEvents:UIControlEventValueChanged]; 
    } 
    return self; 
} 

/* Setup the PageViewManager with an array of UIViews. */ 
- (void)loadPages:(NSArray*)pages 
{ 
    pages_ = pages; 
    scrollView_.delegate = self; 
    pageControl_.numberOfPages = [pages count]; 

    CGFloat pageWidth = scrollView_.frame.size.width; 
    CGFloat pageHeight = scrollView_.frame.size.height; 

    scrollView_.pagingEnabled = YES; 
    scrollView_.contentSize = CGSizeMake(pageWidth*[pages_ count], pageHeight); 
    scrollView_.scrollsToTop = NO; 
    scrollView_.delaysContentTouches = NO; 

    [pages_ enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) 
    { 
     UIView* page = obj; 
     page.frame = CGRectMake(pageWidth * index, 0, 
           pageWidth, pageHeight); 
     [scrollView_ addSubview:page]; 
    }]; 
} 

/* Setup the PageViewManager with an array of UIViewControllers. */ 
- (void)loadControllerViews:(NSArray*)pageControllers 
{ 
    NSMutableArray* pages = [NSMutableArray arrayWithCapacity: 
          pageControllers.count]; 
    [pageControllers enumerateObjectsUsingBlock: 
     ^(id obj, NSUInteger idx, BOOL *stop) 
     { 
      UIViewController* controller = obj; 
      [pages addObject:controller.view]; 
     }]; 

    [self loadPages:pages]; 
} 

- (void)pageControlChanged 
{ 
    pageIndex_ = pageControl_.currentPage; 

    // Set the boolean used when scrolls originate from the page control. 
    pageControlUsed_ = YES; 

    // Update the scroll view to the appropriate page 
    CGFloat pageWidth = scrollView_.frame.size.width; 
    CGFloat pageHeight = scrollView_.frame.size.height; 
    CGRect rect = CGRectMake(pageWidth * pageIndex_, 0, pageWidth, pageHeight); 
    [scrollView_ scrollRectToVisible:rect animated:YES]; 
} 

- (void)scrollViewDidScroll:(UIScrollView*)sender 
{ 
    // If the scroll was initiated from the page control, do nothing. 
    if (!pageControlUsed_) 
    { 
     /* Switch the page control when more than 50% of the previous/next 
      page is visible. */ 
     CGFloat pageWidth = scrollView_.frame.size.width; 
     CGFloat xOffset = scrollView_.contentOffset.x; 
     int index = floor((xOffset - pageWidth/2)/pageWidth) + 1; 
     if (index != pageIndex_) 
     { 
      pageIndex_ = index; 
      pageControl_.currentPage = index; 
     } 
    } 
} 

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView 
{ 
    pageControlUsed_ = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView 
{ 
    pageControlUsed_ = NO; 
} 

@end 

要使用這個類,您將它嵌入一個UIViewController內比包含UIScro llView和UIPageControl。

用法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // Create some views dynamically 
    UIView* v1 = ... 
    UIView* v2 = ... 

    // Put the views inside an NSArray: 
    NSArray* pages_ = [NSArray arrayWithObjects:v1, v2, nil]; 

    /* Create the PageViewManager, which is a member (or property) of this 
     UIViewController. The UIScrollView and UIPageControl belong to this 
     UIViewController, but we're letting the PageViewManager manage them for us. */ 
    pageViewManager_ = [[PageViewManager alloc] 
         initWithScrollView:self.scrollView 
           pageControl:self.pageControl]; 

    // Make the PageViewManager display our array of UIViews on the UIScrollView. 
    [pageViewManager_ loadViews:pages_]; 
} 

我的代碼示例假定您正在使用ARC。

+0

對不起,但我不能使用它沒有scrollview? – 2012-01-29 23:02:51

+1

如果您希望能夠通過使用手指(如iPhone的主屏幕)從一個頁面滾動到下一個頁面,那麼最簡單的方法(我知道的)是使用UIScrollView。當用戶在用戶界面上看到UIPageControl時,他們希望能夠通過輕彈從一個頁面移動到另一個頁面。 – 2012-01-29 23:12:08

+0

謝謝@Emile,我正是這樣問的。 – 2012-01-29 23:21:48

相關問題