ImageView 框架改变条件

ImageView Frame Change Condition

我正在构建一个示例项目,我在其中以编程方式创建 UIImageView 和 UIButton 并在循环内更改其框架。我的情况是,当我点击我的按钮时,imagePicker 会出现,在从库中选择图像后,它将如何在 imageView 中移动,按钮将在图像视图旁边移动。这样,如果我继续选择图像按钮,每次都会移动。我必须连续保留 3 个项目,但我必须在不使用 collectionView 或 tableview 的情况下执行此操作。对于第一行,我的代码工作正常,但是当我第三次选择图像时,它没有按预期工作。截图会更清楚。

这就是我想要的。

第一行像这样工作正常

但是当我第三次点击按钮并select一张图片时,我会变成这样

移动imageView和button的代码是

 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)];

        }

    }

}

我确定在for循环中,我给出的条件是错误的。谁能解决我的问题。非常感谢任何帮助。

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];