【问题标题】:Simple Image Gallery using UIScrollView使用 UIScrollView 的简单图片库
【发布时间】:2013-06-11 08:21:50
【问题描述】:

我正在尝试创建一个非常基本的图片库,其中包含三张图片,用户将能够滑过并查看图片。

我的解决方案正在使用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 中。

【问题讨论】:

    标签: ios objective-c uiscrollview uipagecontrol


    【解决方案1】:

    尝试改变

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

    【讨论】:

    • 你好邦妮!不敢相信我犯了这么愚蠢的错误! XCode 的自动填充功能吸引了我!没有发现我使用了错误的方法。谢谢
    • 你好,在我的情况下,我没有滚动 uiscrollview。我只能查看第一张图片
    【解决方案2】:
    - (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]];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多