0

我有一个滚动器,我希望能够在视图之间滚动,例如像这样的简单图像视图。我在这里设置它们:

    UIImageView *settingsView = [[UIImageView alloc] init];
    [settingsView setImage:[UIImage imageNamed:@"1.jpg"]];

    UIImageView *cameraView = [[UIImageView alloc] init];
    [cameraView setImage:[UIImage imageNamed:@"2.jpg"]];

接下来我想把这些 ImageViews 放入我的 ScrollView 数组中。我试着这样做:

int PageCount = 2;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:settingsView,cameraView,nil];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
[scroller setShowsHorizontalScrollIndicator:NO];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
scroller.delegate = self;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
    UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
    [ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
    [scroller addSubview:ImgView];
    scroller.contentSize = CGSizeMake(width, 0);
    width +=scroller.frame.size.width;
    xPos  +=scroller.frame.size.width;
}

我得到的错误是'NSInvalidArgumentException', reason: '-[UIImageView stringByDeletingPathExtension]: unrecognized selector sent to instance

我怎样才能将这些视图放在滚动条内?谢谢!

4

5 回答 5

0
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:settingsView,cameraView,nil];

应该

NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:@:"1.jpg", @"2.jpg",nil];

您正在将UIImageView对象传递到应该是的对象中NSString

于 2013-10-18T05:19:07.693 回答
0

arrImageName已经imageViews准备好了。

所以这条线是错误的

[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];

你只需要将它们添加imageviewsscrollview.

for (int i=0; i<PageCount; i++)
{
    [scroller addSubview:[arrImageName objectAtIndex:i]];
    scroller.contentSize = CGSizeMake(width, 0);
    width +=scroller.frame.size.width;
    xPos  +=scroller.frame.size.width;
}

并考虑将框架设置为 imageViews。

希望这会帮助你。

于 2013-10-18T05:20:29.760 回答
0

问题是您传递的是UIImageViewtoimageNamed而不是NSString

这一行:

[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];

应该:

UIImageView *imageView = [arrImageName objectAtIndex:i];
[ImgView setImage:imageView.image];
于 2013-10-18T05:20:33.413 回答
0
int PageCount = 2;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:settingsView,cameraView,nil];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
[scroller setShowsHorizontalScrollIndicator:NO];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
scroller.delegate = self;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
    UIImageView *ImgView = [arrImageName objectAtIndex:i];
    [ImgView setFrame:CGRectMake(xPos, 0, scroller.frame.size.width,  scroller.frame.size.height)];

    [scroller addSubview:ImgView];
    scroller.contentSize = CGSizeMake(width, 0);
    width +=scroller.frame.size.width;
    xPos  +=scroller.frame.size.width;
}
于 2013-10-18T05:28:47.463 回答
0
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]init];
ImgView =[arrImageName objectAtIndex:i];
[ImgViewSetFrame:CGRectMake(xPos, 0,   scroller.frame.size.width, scroller.frame.size.height)]
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos  +=scroller.frame.size.width;
}
于 2013-10-18T10:36:19.447 回答