我正在尝试将多个添加UIImageView
到scrollView
. imageview 的数量不同。这个数字是从文件中读取的。
所以现在我必须创建UIImageView
*imageView1, UIImageView
*imageView2, UIImageView
*imageView3 ... UIImageView
*imageViewN。
如何将数字添加到*imageView(?)的末尾
任何帮助将不胜感激
我正在尝试将多个添加UIImageView
到scrollView
. imageview 的数量不同。这个数字是从文件中读取的。
所以现在我必须创建UIImageView
*imageView1, UIImageView
*imageView2, UIImageView
*imageView3 ... UIImageView
*imageViewN。
如何将数字添加到*imageView(?)的末尾
任何帮助将不胜感激
这不能按照您的想法进行。你应该做的是像这样使用 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];
这也使您的代码更加灵活。
添加到上面的代码片段:
NSString *imageName = [NSString stringWithFormat@"test_%d.png", i];
[currentImageView setImage:[UIImage imageNamed:imageName]];
因此您可以添加具有不同图像的 UIImageViews
对于最近仍在搜索此主题的任何人,这是 Swift 3 版本,可以满足@Siddharthan 的要求:
//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)