2013-05-26 52 views
1

我需要幫助我試圖創建的應用程序,其中我有一個UIScrollView三個pages水平UIScrollView和一堆UIButtons

問題是,buttons'FOO'和'BAR'被加載到第一頁,而應該只加載到第二頁。

因此,該項目應該是這樣的:

\--View (The underlying UIView) 
    \--scrollView (The UIScrollView) 
     \--button (The buttons) 

- (void)createButton 
{ 
    float xButton = 10.0; 
    NSArray *buttonTitles = @[@"foo", @"bar"]; 

    for (int i = 0; i < 2; i++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(xButton, 10.0, 100.0, 50.0); 

     [button setTitle:buttonTitles[i] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; 
     [scrollView addSubview:button]; 
     [button setTag:i]; 

     xButton += 200; 
    } 
} 

我有一個UIView有三個水平UIScrollViews。我想要做的是,按鈕將顯示在第二個視圖中。但每次啓動應用程序時,按鈕都已加載到第一個視圖中,而不是第二個視圖中。

__________  __________  __________ 
|  |  |  |  |  | 
| 1st | <--- | 2nd | <--- | 3rd | 
| page | scroll | page | scroll | page | 
|  |  |  |  |  | 
|________|  |________|  |________| 

如何將按鈕加載到第二頁,並且不會再出現在第一頁和第三頁中?看來通過檢查currentPageUIPageControl將不起作用。

此外,爲了在我啓動應用程序時首先顯示第二頁,需要做些什麼?

回答

1

這裏有幾件事情:

我有三個水平UIScrollViews

一個UIView您應該只有一個UIScrollView。它可能有內容的頁,但應該只有一個啓用分頁的滾動視圖。

如何將按鈕加載到第二個視圖中,而不是在第一個視圖中?

難道你真的不想加載所有的三頁按鈕?這樣,如果用戶從第2頁開始並向左滾動,則第1頁將加載其按鈕。如果他們向右滾動,第3頁將已經加載了它的按鈕。

如果您真的覺得您需要推遲加載下一頁的按鈕,直到用戶開始滾動,您可以實現UIScrollViewDelegate協議,並在scrollViewDidScroll:中檢測滾動。當滾動開始時,你將不得不加載新的按鈕。

但是,我不會推薦這個。像這樣按需加載可能會使滾動更加遲鈍(如果您aren't careful about performance),並且按鈕通常不是大內存用戶,所以我認爲您可以隨時輕鬆保留加載的所有3頁按鈕。

- (void)createButton 
{ 
    float xButton = 10.0; 
    NSArray *buttonTitles = @[@"foo", @"bar"]; 

    for (int i = 0; i < 2; i++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(xButton, 10.0, 100.0, 50.0); 

     /* button setup here */ 

     xButton += 200; 
    } 
} 

我不認爲你想增加每次迭代xButton 200點。 SpringBoard不會像這樣放置按鈕。我一旦建成什麼樣的是模仿跳板的佈局,代碼初始化按鈕是這樣的:

const int PADDING = 4; 
const int SCREEN_WIDTH = 320; 
const int SCREEN_HEIGHT = 411; 
const int SPACING = (SCREEN_WIDTH - (4 * 57))/5; // 5 spaces between 4 columns 

float x = SPACING; 
float y = SPACING; 

int index = 0; 
int page = 0; 

for (NSString* title in buttonTitles) { 

    UILabel* btnLabel = [[UILabel alloc] initWithFrame: CGRectMake(x, y + 57 + PADDING, 57, 20)]; 
    btnLabel.text = title; 
    btnLabel.font = [UIFont boldSystemFontOfSize: 12.0]; 
    btnLabel.shadowColor = [UIColor darkGrayColor]; 
    btnLabel.shadowOffset = CGSizeMake(1, 1); 
    btnLabel.textColor = [UIColor whiteColor]; 
    btnLabel.opaque = NO; 
    btnLabel.backgroundColor = [UIColor clearColor]; 
    btnLabel.textAlignment = UITextAlignmentCenter; 

    [self.scrollView addSubview: btnLabel]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(x, y, 57, 57); 
    // NOTE: my code uses labels beneath buttons, not button titles 
    //[button setTitle:title forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.scrollView addSubview:button]; 
    // index tag helps us determine which button was pressed later 
    [button setTag:index]; 

    // I keep a NSMutableSet of all the buttons 
    [self.buttons addObject: button]; 

    if ((x + (2 * 57) + SPACING) > (SCREEN_WIDTH * (page + 1))) { 
     // new row 
     x = SCREEN_WIDTH * page + SPACING; 
     if ((y + (2 * 57) + SPACING) > SCREEN_HEIGHT) { 
      // new page (to the right of the last one) 
      page++; 
      y = SPACING; 
      x = SCREEN_WIDTH * page + SPACING; 
     } else { 
      y += 57 + PADDING + 20 + SPACING; 
     } 
    } else { 
     x += 57 + SPACING; 
    } 
    index++; 
} 

// set the virtual scrollView size to allow paging/scrolling horizontally 
self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (page + 1), SCREEN_HEIGHT); 

在我的代碼,我觸發了觸摸的內心事件單擊按鈕回調,如我認爲這可以帶來更好的用戶體驗。

我還添加了標籤低於我的按鈕,不作爲按鈕的一部分(再次,模仿SpringBoard本身)。如果您願意,您可以刪除該代碼(btnLabel)。

此外,爲了在我啓動應用程序時首先顯示第二個視圖,需要做些什麼?

如果你有一個滾動視圖,並希望開始第2頁,使用currentPageUIPageControl

- (void)viewWillAppear: (BOOL) animated { 
    [super viewWillAppear: animated]; 
    self.pageControl.currentPage = 1; // for page '2' 
    } 

注:我似乎記得57點ISN」實際上SpringBoard的大小顯示圖標。它的大小如下,忽略了陰影/邊緣效應,我認爲它的總大小爲60分。無論如何,你可以玩。

+0

嗨。首先,我想感謝您的快速回復。 二,對於混淆抱歉。我只有**一個''UIScrollView'和**三個頁面,就像你所說的。 第三,第一頁,第二頁和第三頁下的按鈕有不同的目的和屬性,因此在三頁中加載所有按鈕是一種否定的方式。 – Jahm

+0

SpringBoard的應用程序圖標都有不同的目的和屬性(標題和圖像)。爲什麼將所有按鈕加載在一起會導致問題? – Nate

+0

謝謝!這澄清了它。 – Jahm