我正在尝试向UIImageView
添加多个scrollView
。 imageview的数量不同。此编号从文件中读取。
所以现在我必须创建 UIImageView
* imageView1,UIImageView
* imageView2,UIImageView
* imageView3 ... UIImageView
* imageViewN 。
如何将数字添加到 * imageView(?)
的末尾非常感谢任何帮助
答案 0 :(得分:2)
这不能按照你的想法来完成。你应该做的是使用像这样的NSMutableArray:
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for(NSInteger i=0; i < 10; i++)
{
UIImageView *currentImageView = [[UIImageView alloc] initWithFrame:CGRectMake((0 * (110 * i)), 0, 100, 100)];
[currentImageView setImage:[UIImage imageNamed:@"test.png"]];
[imageViews addObject:currentImageView];
}
//To reference the image views just call objectAtIndex
UIImageView *imageView1 = [imageViews objectAtIndex:0];
UIImageView *imageView2 = [imageViews objectAtIndex:1];
这也使您的代码更加灵活。
答案 1 :(得分:0)
添加到上面的代码片段:
NSString *imageName = [NSString stringWithFormat@"test_%d.png", i];
[currentImageView setImage:[UIImage imageNamed:imageName]];
因此您可以添加具有不同图像的UIImageViews
答案 2 :(得分:0)
对于最近仍在搜索此主题的人来说,这是执行@Siddharthan所希望的Swift 3版本:
//you may give any Int value to these variables
let Xposition = 19
let Yposition = 19
let imageWidth = 93
let imageHeight = 128
//create UIImageView object
var curr_imageView = UIImageView()
for i in 0 ..< images.count {
if imageViews.isEmpty {
curr_imageView = UIImageView.init(frame: CGRect(x: Xposition, y: Yposition, width: imageWidth, height: imageHeight))
} else {
curr_imageView = UIImageView.init(frame: CGRect(x: Int(imageViews[i-1].frame.origin.x)+Int(imageViews[i-1].frame.size.width), y: Yposition, width: imageWidth, height: imageHeight))
}
curr_imageView.image = UIImage(named: images[i])
curr_imageView.contentMode = .scaleAspectFit
self.scrollView1.addSubview(curr_imageView)
imageViews.append(curr_imageView)
}
self.scrollView1.contentSize = CGSize(width: imageViews[imageViews.count-1].frame.size.width+imageViews[imageViews.count-1].frame.origin.x+CGFloat(Xposition), height: self.scrollView1.frame.size.height)