2012-11-22 34 views
1

我滾動查看許多圖像按鈕,按鈕1-8沒有問題的按鈕行爲。它可以是可點擊的日誌。UIScrollview查看第二頁按鈕沒有聽到觸摸

問題是第二頁上的按鈕看起來像不聽選擇器(按鈕9-10)。頁面控制可能存在問題?或者我在這裏錯過了什麼?

// button paging 
myImages = [NSArray arrayWithObjects: 
        [UIImage imageNamed:@"img_1.png"], 
        [UIImage imageNamed:@"img_2png"], 
        [UIImage imageNamed:@"img_3.png"], 
        [UIImage imageNamed:@"img_4.png"], 
        [UIImage imageNamed:@"img_5.png"], 
        [UIImage imageNamed:@"img_6.png"], 
        [UIImage imageNamed:@"img_7.png"], 
        [UIImage imageNamed:@"img_8.png"], 
        [UIImage imageNamed:@"img_9.png"], 
        [UIImage imageNamed:@"img_10.png"], 
        nil 
      ]; 

// prepare counter 
int posX, posY = 5, counter = 0, pages = 0; 
BOOL reset = NO; 

// uiviewscroller 
CGRect scrollFrame; 
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages; 
scrollFrame.origin.y = 0; 
scrollFrame.size = self.myScrollView.frame.size; 

UIView *subView = [[UIView alloc] initWithFrame:scrollFrame]; 
subView.backgroundColor = [UIColor colorWithRed:232.0f/255.0f green:233.0f/255.0f blue:235.0f/255.0f alpha: 1]; 

// loop over images 
for (int i = 0; i < myImages.count; i++) 
{ 
    counter++; 

    // reposition X 
    if (counter == 1 || counter == 5) 
    { 
     posX = 4; 

     // more than 1 page 
     if (pages > 0) 
      posX = 320 * pages; 
    } 
    else 
    { 
     posX += 80; 
    } 

    // reposition Y 
    posY = (counter <= 4) ? 5 : 85; 

    if (reset == YES) 
    { 
     // reflag 
     reset = NO; 

     // redefined size 
     scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;    
    } 

    // create image view for button 
    UIImage *myIcon = [myImages objectAtIndex:i]; 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    // manipulate button behavior 
    myButton.frame = CGRectMake(posX, posY, 80, 80); 
    [myButton setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal]; 
    [myButton setImage:myIcon forState:UIControlStateNormal]; 
    [myButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    // add image to subview 
    [subView addSubview:myButton]; 

    if (counter == 8) 
    { 
     // reset position 
     posX = 4; 
     posY = 5; 

     // populate counter 
     counter = 0; 
     pages++; 

     // reset section 
     reset = YES; 

     // add subview to view 
     [self.myScrollView addSubview:subView]; 
    } 

    // add balance subview to scroller 
    if (counter > 0 && i == myImages.count-1) 
    { 
     // add subview to view 
     [self.myScrollView addSubview:subView]; 
     pages++; 
    } 
} 

scrollFrame.origin.x = self.myScrollView.frame.size.width * pages; 
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * pages, self.myScrollView.frame.size.height); 
self.myPaging.numberOfPages = pages; 

回答

0

您正在一個名爲subView的視圖上添加按鈕。因爲視圖的框架等於滾動視圖的框架(UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];),所以最後兩個按鈕被添加到該視圖的邊界之外。

加入buttons-

在末端添加下列行,其中要更改滾動視圖的內容大小後改變其幀。

[subView setFrame:CGRectMake(subView.frame.origin.x, subView.frame.origin.y, _scrollView.frame.size.width * pages, _scrollView.frame.size.height)]; 
+0

謝謝,它的工作。所以我也嘗試刪除以下代碼。 scrollFrame.origin.x = self.myScrollView.frame.size.width * pages; –