2013-01-04 48 views
1

我使用imageview和半透明按鈕滾動查看此圖像。 按鈕和imageview被添加到[addsubview]滾動視圖。 現在我需要改變的ImageView和按鈕位置(改變幀屬性)UIButton在滾動視圖中更改位置時隱藏

ES:

button.frame = CGRectMake(x, y, w, h); 

我做這個按鈕後,皮張。如果我只改變按鈕的位置,它可以很好地工作。 PS:我剛剛嘗試過使用前置攝像頭。

滾動視圖內容製作:

int index = 0; 
    int x = OFFSETXCOPERTINA; 
    int y = 0; 
    int w = LARGHEZZACOPERTINA; 
    int h = ALTEZZACOPERTINA; 
    int riga = 0; 
    int indexriga = 0; 
    int testindex = 0; 

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown) { 
     testindex = NRCopertineVerticale; 
    } 
    else { 
     testindex = NRCopertineOrizzontale; 
    } 

    for (NSDictionary *rigaordine in ElencoOrdini) 
    { 

     if (indexriga > 0) x = x + OFFSETXCOPERTINA + w; 
     y = OFFSETYCOPERTINA + (riga * (h + OFFSETYCOPERTINA)); 

     UIButton *buttonCopertina = [UIButton buttonWithType:UIButtonTypeCustom]; 
     buttonCopertina.frame = CGRectMake(x, y, w, h); 

     [buttonCopertina setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(0/255.0) alpha:0.3]]; 

     UIImage *image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat: @"%@image.php?CODe=%@", URLXXX, [rigaordine objectForKey:@"cod_isb"]]]]]; 

     UIImageView *copertina = [[UIImageView alloc] initWithImage: image]; 
     [copertina setTag:TAGCopertine + index]; 
     [copertina setFrame:CGRectMake(x, y, w, h)]; 

     [buttonCopertina setTag:TAGButtonCopertine + index]; 
     [buttonCopertina addTarget:self action:@selector(buttonCopertinaClicked:) forControlEvents:UIControlEventTouchUpInside]; 

     [scrollView addSubview:copertina]; 
     [scrollView addSubview:buttonCopertina]; 

     index++; 
     indexriga++; 

     if (indexriga >= testindex) { 
      indexriga = 0; 
      riga++; 
      x = 10; 
     } 

    } 

當我旋轉ipad公司:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    int index = 0; 
    int x = OFFSETXCOPERTINA; 
    int y = 0; 
    int w = LARGHEZZACOPERTINA; 
    int h = ALTEZZACOPERTINA; 
    int riga = 0; 
    int indexriga = 0; 
    int testindex = 0; 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown) { 
     testindex = NRCopertineVerticale; 
    } 
    else { 
     testindex = NRCopertineOrizzontale; 
    } 

    for(int j = 0; j< 999; j++) { 
     if (indexriga > 0) x = x + OFFSETXCOPERTINA + w; 
     y = OFFSETYCOPERTINA + (riga * (h + OFFSETYCOPERTINA)); 

     UIImageView *copertina = (UIImageView *)[self.scrollView viewWithTag:(TAGCopertine + j)]; 
     if (copertina != nil) { 
      copertina.frame = CGRectMake(x, y, LARGHEZZACOPERTINA, ALTEZZACOPERTINA); 
     } 

     UIButton *button = (UIButton *)[self.scrollView viewWithTag:(TAGButtonCopertine + j)]; 
     if (button != nil) { 
      button.frame = CGRectMake(x, y, LARGHEZZACOPERTINA, ALTEZZACOPERTINA); 
     } 

     index++; 
     indexriga++; 

     if (indexriga >= testindex) { 
      indexriga = 0; 
      riga++; 
      x = 10; 
     } 
    } 


} 
+0

你可以發佈你的代碼嗎? –

+0

UIButton和UIScrollView之間的關係是什麼? – Stavash

+0

如何添加imageView和按鈕顯示您的代碼,以及如何更改按鈕的框架? – TheTiger

回答

0

可能是你正在設置按鈕的一些不正確的框架和它去屏幕外。這裏的正確方法是創建一個UIView子類,其中有UIImageViewUIButton。公開一個屬性設置和圖像,並創建一個delegate觸摸按鈕。然後,使用按鈕實例化您的視圖,設置圖像,設置其代表並將其添加爲UIScrollView的子視圖,並只用按鈕更改視圖的框架 - 所有內容都將相應地重新定位。

+0

框架是正確的,如果我不改變UIImageView框架按鈕正確地改變位置。 – MatteoC

+0

我試過這種方法。我有同樣的問題。 – MatteoC

0

經過數千次測試,我發現的唯一方法是將UIImageViews的位置更改爲willRotateToInterfaceOrientation。

並在更改了didRotateFromInterfaceOrientation中的UIButtons位置之後。

這似乎沒有解釋,但它的工作原理。

相關問題