【问题标题】:ImageView Frame Change ConditionImageView 帧变化条件
【发布时间】:2015-04-14 07:42:50
【问题描述】:

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

这就是我想要的。

对于第一行,它像这样工作正常

但是当我第三次点击按钮并选择图像时,我会变成这样

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 循环中,我给出的条件是错误的。谁能纠正我的问题。非常感谢任何帮助。

【问题讨论】:

    标签: ios objective-c uiimageview


    【解决方案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];
    

    【讨论】:

    • @iPeter - 如果您需要更多说明或帮助,请告诉我。
    • 我保证!在过去的 3 天里,我陷入了这个问题。 @丹尼尔斯
    • 你理解我的问题有点错误。每次选择图像后,我的按钮都会移动。
    • 我假设您编写的代码是在每次添加图像后执行的(使用选择器选择图像)。然后,您应该定位所有图像,然后定位按钮(这是我的代码所做的)。如果我误解了你,请提供更多解释(可能附上更多代码)
    • 好的。让我更简短地解释一下。首先,当您运行项目时,首先会显示该按钮。你点击它,选择器来了。你选择一个图像。然后在关闭选择器后,所选图像将出现在 imageview 中。现在,图像视图的位置是按钮最初所在的位置,现在按钮的位置就在图像视图旁边,有边距。现在以这种方式,每次您选择图像时,图像都会在另一个旁边显示,并且每次图像视图都会移动按钮。现在你明白我到底需要什么了吗? @丹尼尔斯
    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 2017-05-01
    • 2020-09-23
    • 2012-08-19
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多