如何更改UIScrollView上的图像

时间:2012-03-23 11:33:01

标签: iphone uiscrollview

我有一个包含多个图像的滚动视图。图像是可滚动的。

我使用以下代码:

scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

scrollView.pagingEnabled = YES;

scrollView.delegate = self;

scrollView.showsVerticalScrollIndicator = NO;

scrollView.showsHorizontalScrollIndicator = NO;


NSInteger numberOfViews = [arrayImage count];

for (int i = 0; i < numberOfViews; i++)
{
    CGFloat yOrigin = i * scrollView.frame.size.width;
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin  +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)];
    imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
    [scrollView addSubview:imageView];
    [imageView release];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0);
[self.view addSubview:scrollView];
[scrollView release]

该应用程序将与iPad方向一起使用,但图像未在横向模式下正确滚动。

它在纵向模式下工作正常,但图像以横向模式合并。

如果有人有任何想法,请分享....

2 个答案:

答案 0 :(得分:1)

试试这个...

scrollView.delegate = self;
scrollView.scrollEnabled = YES;

int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);     


int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];

for(int index=0; index < [imagesName count]; index++)
{       
    UIImageView *img = [[UIImageView alloc] init];
    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(5+xOffset, 0, 50, 50);
    NSLog(@"image: %@",[imagesName objectAtIndex:index]);
    img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
    [images insertObject:img atIndex:index];         
    scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
    [scrollView addSubview:[images objectAtIndex:index]];

    xOffset += 70;
}

设置此...

imagesName = [[NSArray alloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.png",@"image7.png",@"image9.png",nil];
images = [[NSMutableArray alloc]init];

答案 1 :(得分:0)

每次界面方向改变时都必须为此目的设置图像框,或者必须在方向改变后再次重绘图像