3
UIView *view = nil;

NSArray *subviews = [scrollView subviews];

CGFloat curXLoc = 0;

for (view in subviews)
{
    if ([view isKindOfClass:[UIView class]] && view.tag > 0)
    {
        CGRect frame = view.frame;

        frame.origin = CGPointMake(curXLoc, 0);

        view.frame = frame;


        curXLoc += (kScrollObjWidth);
    }
}

我放了一个Scrollview,在这个滚动视图中我把 5UIImageview放在 XIB 文件中

我有一个图像和一个按钮,每当我单击按钮时,图像都应该以编程方式加载到程序中的所有UIImageviews 中UIScrollview

我还使用了以下编码

for(UIImageView *addview in Scroll)
{

    if([addview isKindOfClass:[UIImageView class]])
    {

        UIImageView *newImage = (UIImageView *)addview;

        newImage.image = [UIImage imageNamed:@"EarP010.jpg"];
    }
}
4

3 回答 3

3

试试这个,

NSMutableArray * imagesArray = [[NSMutableArray alloc]init];

for (int imageCount = 0; imageCount < (noOfImages);imageCount++)
{
 [imagesArray addObject:[UIImage imageNamed:@"localImagePath%d.png",imageCount]];
}

UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,480.0,960.0)];
scrollview.contentSize = CGSizeMake(scrollview.size.width * (noOfImages),scrollview.frame.size.height);
scrollview.pagingEnabled = YES;

CGFloat xPos = 0.0;

for (UIImage * image in imagesArray) {
@autoreleasepool {
UIImageView * imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(xPos, 0.0,scrollview.frame.size.width ,self.scrollView.frame.size.height);
[scrollview addSubview:imageview];
xPos += scrollview.size.width;
}
}
于 2013-01-28T12:39:06.690 回答
1

如果您知道需要多少图像视图,则可以在 while 循环中添加图像视图。

     UIScrollView *sv = [UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     ....
     [self.view addSubView:sv];

     int i = 0;
     while(i < numberOfImages){
         i = i + 200;
         UIImageView *i = [UIImageView alloc] initWithFrame:CGRectMake(20, i, 200, 100)]
         ....
         [sv addSubView:i];
       i++;
     }

这是您可以使用的一种情况。您将需要知道所需的图像视图数量以及这些图像视图上的 Y 值。这段代码大部分是伪代码,需要填写。

于 2013-01-27T06:22:41.480 回答
1

用这个

NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"your xib name" owner:nil options:nil];

UIView* mainView = [objects objectAtIndex:0];

for (UIView* view in [mainView subviews]) {
    if([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *iview = (UIImageView *)view;


        iview.image = [UIImage imageNamed:@"your imagename.png"];
    }
}
于 2013-01-27T06:32:11.493 回答