繁体   English   中英

如何在UIScrollView上更改图像

[英]How to change the image on 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方向上使用,但是在横向模式下无法正确滚动图像。

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

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

试试这个...

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];

每次界面方向更改时,您都必须为此设置图像框架,或者在方向更改后必须重新绘制图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM