我正在尝试创建一个非常基本的图库,其中包含三个图像,用户可以滑过并查看图像。
我的解决方案正在使用UIColor
NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor redColor], [UIColor greenColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [colors count], 208);
但图像不会显示类似的解决方案
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:@"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3c.jpg"], nil];
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [images objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [images count], 208);
有什么想法吗? UIScrollView
内存有UIView
。
答案 0 :(得分:2)
尝试并更改
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:@"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3c.jpg"], nil];
到
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"pic_1_3a.jpg"], [UIImage imageNamed:@"pic_1_3b.jpg"], [UIImage imageNamed:@"pic_1_3c.jpg"], nil];
答案 1 :(得分:0)
- (void)viewDidLoad
{
arrImages=[[NSArray alloc]initWithObjects: @"bigbazaar_card.jpg", @"CouponImage.png", @"futureBazaar_card.jpg",@"No_deal.png",@"Reliance_card.jpg",@"wallmart_card.jpg",@"westside_card.jpg", nil];
int x=5;
int y=5;
for (int i=0; i<[arrImages count]; i++) {
NSString *str=[arrImages objectAtIndex:i];
UIImageView *currentimage=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[currentimage setImage:[UIImage imageNamed:str]];
[scroll addSubview:currentimage];
UIButton *btnTag=[[UIButton alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[btnTag setTitle:@"" forState:UIControlStateNormal];
selectedImageIndex=i;
btnTag.tag=i;
NSLog(@"btntag-->%i",btnTag.tag);
[btnTag addTarget:self action:@selector(BtnShowImage:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:btnTag];
x= x+170;
}
scroll.contentSize = CGSizeMake(170* arrImages.count,scroll.frame.size.height);
}
你必须在.h文件中声明UIScrollView的委托,如下所示:
@interface ViewController : UIViewController<UIScrollViewDelegate>
and you have to implement ScrollView's Delegate method if you want to display image on click as shown below:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x += scrollView.frame.size.width / 2;
NSInteger index = contentOffset.x / scrollView.frame.size.width;
NSString *strTemp=[arrImages objectAtIndex:index];
[imageData setImage:[UIImage imageNamed:strTemp]];
}