2013-04-28 51 views
0

現在我正在實現一個UIScrollView,我面臨着一個我從來沒有看到的問題:在我的滾動視圖上的3個UIButton中有2個沒有響應我的點擊。 其中一個沒有,但另一個沒有。UIScrollView&UIButtons,真是奇怪的行爲

我做了很多的研究,並嘗試了很多東西(試圖削減的延遲,設置用戶交互......)

在viewDidLoad方法:

BoutonPig1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[BoutonPig1 setFrame:CGRectMake(0, 45, 145, 90)]; 
[BoutonPig1 addTarget:self action:@selector(BonusShop) forControlEvents:UIControlEventTouchUpInside]; 
UIImage * boutonPig1OnImage = [UIImage imageNamed:@"Pig2ShopBloquee.png"]; 
[BoutonPig1 setImage:boutonPig1OnImage forState:UIControlStateNormal]; 

我一套scrol視圖方法用3個元素:

-(void)setScrollView:(UIScrollView *)scroller1 
{ 
for (int i = 0; i < 3; i++) 
{ 
    CGRect frame; 
    frame.origin.x = 0; 
    frame.origin.y = scroller.frame.size.height * i; 
    frame.size = scroller.frame.size; 

    UIImageView *newPageView = [[UIImageView alloc] init]; 
    newPageView.contentMode = UIViewContentModeScaleAspectFit; 
    newPageView.frame = frame; 

    if (i == 0) 
    { 
     [newPageView addSubview:BoutonPig1]; 
    } 
    if (i == 1) 
    { 
     [newPageView addSubview:BoutonPig2]; 
    } 
    if (i == 2) 
    { 
     [newPageView addSubview:BoutonPig3]; 
    } 
    [newPageView setUserInteractionEnabled:YES]; 
    [scroller addSubview:newPageView]; 
} 
scroller.contentSize = CGSizeMake(scroller.frame.size.width * 3, scroller.frame.size.height); 
} 

,我放在子視圖的三個元素是3個UIButtons像在第一代碼提取物。做同樣的事情。現在唯一不同的是他們的立場。

[BoutonPig2 setFrame:CGRectMake(0, 164, 145, 90)]; 
    [BoutonPig3 setFrame:CGRectMake(0, 285, 145, 90)]; 

感謝您的幫助!

+0

什麼是你的UIScrollView的框架?我想象一下你的scrollview的內容大小不夠大tbh。 – Dev2rights 2013-04-28 19:31:04

+0

scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(59.0,54.0,145,180)]; – user2057209 2013-04-28 19:39:15

+0

那麼就有答案了,180 + 54 = 234對於初學者來說按鈕3上的值大於285。調出滾動視圖的框架。也不知道爲什麼你需要該圖像視圖,因爲你不設置圖像。 – Dev2rights 2013-04-28 19:43:59

回答

0

我認爲問題出在座標,因爲你添加按鈕BoutonPig2 BoutonPig3 newPageView座標CGRectMake(0,164,145,90)CGRectMake(0,285,145,90),但你需要設置CGRectMake (0,45,145,90)或CGRectMake(0,0,145,90)...因爲您不添加按鈕到scool上,而是在newPageView上,並且您有新的座標0,0