UIScrollView和滑动图像

时间:2012-02-03 11:23:31

标签: iphone objective-c xcode uiscrollview

我有一个数组imageNames

NSArray *imageNames = [[NSArray alloc] initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", nil];

我想在UIScrollView中显示第一张图片,然后在制作水平幻灯片时更改它。

.h代码:

@interface SlidePViewController : UIViewController <UIScrollViewDelegate>
{       
    IBOutlet UIScrollView *scrollView;  
    UIImageView *imageView; 
}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;

@end

我创建了这个应该完成工作的代码

for(NSString *imageName in imageNames){
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    image.frame = CGRectMake([imageNames indexOf:imageName] * SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
    [scrollView addSubview:image];
    [image release];
}

我用数字

更改了SCREENWIDTH和SCREENWIDTH ....
image.frame = CGRectMake([imageNames indexOf:imageName] * 100, 0, 11, 888);

错误:

  

二进制表达式的无效操作数('id'和'int')

我不确定此代码是否符合我的要求:/

更新 我改变这条线

image.frame = CGRectMake([imageNames indexOfObject:imageName] * 1000, 0, 1100, 888);

错误已修复,但我无法进行滑动,第一张图像显示在UIScrollView中,并且不会随水平幻灯片而改变。

3 个答案:

答案 0 :(得分:0)

下面的行可能有错误。

image.frame = CGRectMake([imageNames indexOf:imageName] * SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);

在上面尝试image.frame = CGRectMake(320, 0, SCREENWIDTH, SCREENHEIGHT);

如果执行它而没有错误,则表示您需要更改框架。

答案 1 :(得分:0)

image.frame = CGRectMake([imageNames indexOfObject:imageName] * 100, 0, 11, 888);

答案 2 :(得分:0)

NSArray文档中不存在indexOf:方法,您应该尝试调用:

- (NSUInteger)indexOfObject:(id)anObject