1

我正在使用其中一个应用程序,在该应用程序中,只有一个视图上有图像。我希望能够向左或向右滑动,让第一个图像离开视图,第二个图像进入。图像是不同的,我希望它看起来像它们是连接的。我在 xib 文件上使用了 UIScrollView 和 UIImageView 下面是我在视图中使用的代码确实加载方法。当我运行应用程序时,滚动视图中只显示最后一个图像。

NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"iPad.png"],[UIImage imageNamed:@"iPadMini.png"],[UIImage imageNamed:@"iPhone4.png"],[UIImage imageNamed:@"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    imgView.frame = frame;
    imgView.image = [images objectAtIndex:i];
    [scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);    
4

2 回答 2

2

尝试使用此代码块...

NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"iPad.png"],[UIImage imageNamed:@"iPadMini.png"],[UIImage imageNamed:@"iPhone4.png"],[UIImage imageNamed:@"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++)
{

    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.frame = CGRectMake(100*i, 0, 100, self.scrollView.frame.size.height);
    imgView.image = [images objectAtIndex:i];
    [scrollView addSubview:imgView];

}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
于 2013-01-25T07:03:15.400 回答
0

试试这个..

 NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"iPad.png"],[UIImage imageNamed:@"iPadMini.png"],[UIImage imageNamed:@"iPhone4.png"],[UIImage imageNamed:@"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
UIImageView *imgView = [[UIImageView alloc]init];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView.frame = frame;
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height); 

希望能帮助到你...

于 2013-01-25T07:05:00.900 回答