2015-04-14 64 views
0

我正在構建一個示例項目,我正在創建一個UIImageView和UIButton以編程方式並在循環內更改其框架。我的情況是,當我點擊我的按鈕imagePicker會來,並從圖書館採摘圖像後,然後它將如何在圖像查看和按鈕將轉移圖像視圖旁邊。這樣如果我繼續選擇圖像按鈕,每次都會改變。我必須在一行中保留3個項目,但我必須在不使用collectionView或tableview的情況下執行此操作。對於第一行,我的代碼工作正常,但是當我第三次選擇圖像時,它不能按預期工作。截圖將使其更加清晰。ImageView框架更改條件

這就是我想要的。

enter image description here

第一行是工作的罰款這樣

enter image description here

但是當我輕點按鈕第三次,並選擇一個圖像,然後我越來越喜歡這個

enter image description here

將imageView和噸是

int x = 8; 
int y = 8; 
int margin = 9; 

for (int i=1;i<=self.arrImages.count;i++){ 
if (i%2 == 0 && i%3!=0) { 

    [myImage setFrame:CGRectMake(x+margin+95.0, y, 95.0, 95.0)]; 
    [_addPhotoButton setFrame:CGRectMake((x+margin+95.0)*2, y, 95.0,95.0)]; 
    x = 8; 
    y = y + margin + 95.0; 

} 
else if (i%3 == 0 && i%2 !=0) { 
    [myImage setFrame:CGRectMake(x+margin+95.0+95.0+margin, y, 95.0,95.0)]; 
    [_addPhotoButton setFrame:CGRectMake(x, y+95.0+margin, 95.0, 95.0)]; 
    x = 8; 
    y = y + margin + 95.0; 

    } 
    else { 

     [myImage setFrame:CGRectMake(x, y, 95.0, 95.0)]; 
     [_addPhotoButton setFrame:CGRectMake(x+margin+95.0, y, 95.0, 95.0)]; 

     } 

    } 

} 

我相信內循環,我給出的條件是錯誤的。任何人都可以請糾正我的問題。任何幫助,高度讚賞。

回答

1
int margin = 9; 
CGFloat size = 95.0; 

UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
[self.arrImages addObject:chosenImage]; 


UIImageView *myImage = [[UIImageView alloc] initWithImage:chosenImage]; 


myImage.frame = _addPhotoButton.frame; 
[self.scrollView addSubview:myImage]; 

CGFloat x,y; 
if(self.arrImages.count %3 == 0) { 
    x = 8; 
    y = _addPhotoButton.frame.origin.y + _addPhotoButton.frame.size.height + margin; 

} 
else { 
    x = myImage.frame.origin.x + size + margin; 
    y = _addPhotoButton.frame.origin.y; 
} 
_addPhotoButton.frame = CGRectMake(x, y, _addPhotoButton.frame.size.width, _addPhotoButton.frame.size.height); 

[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, _addPhotoButton.frame.size.height + _addPhotoButton.frame.origin.y + 8)]; 

[self.scrollView layoutIfNeeded]; 
[picker dismissViewControllerAnimated:YES completion:NULL]; 
+0

@iPeter - 讓我知道你是否需要更多的澄清或幫助。 – DanielS

+0

我保證!最近3天我陷入了這個問題。 @DanielS – iPeter

+0

你明白我的問題有點不對。每次選擇圖像後,我的按鈕都在移動。 – iPeter