【问题标题】:How to load bundle of images in a scrollview with horrizondal scrolling in iphone?如何在 iphone 中通过水平滚动在滚动视图中加载图像包?
【发布时间】:2013-02-15 14:23:05
【问题描述】:

我有一个 iPhone 应用程序,我需要在其中加载大量图像(即大约 50 个),它有一个小矩形(略高于缩略图),每行包含 3 个,一页有 4 列,我需要水平滚动以加载接下来的 12 张图像,依此类推。我还需要将其下的页码显示为 1,2 etc.

任何人都可以引导我朝着正确的方向在滚动视图中实现这一点吗?我真的一团糟,如何将图像视图添加到这样的滚动视图中,每个图像视图也有自己的按钮。

【问题讨论】:

  • 对于水平表视图请检查这个 url..raywenderlich.com/4723/…
  • 图片来自哪里?
  • 来自我的数组。它是从我的包创建的
  • 所以,你必须创建一个视图控制器,在它的初始化器中接收这个数组。然后创建一个自定义视图,以您认为合适的任何方式显示您的图像。您创建这些自定义视图的数组,并使用我在下面的答案中编写的代码在它们之间提供滚动。希望这很清楚。

标签: iphone ios ipad uiscrollview


【解决方案1】:

我解决这个问题的方法如下:(我不会提供很多细节,因为它们取决于您的特定需求和要求)。

.h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet UIScrollView *scrollView;//assuming the scrollView is connected in a xib file

-(id)initWithImages:(NSArray*)images;//that could be whatever format you get your images(UIImage, urls to download, file names or custom views. Here we assume that the array contains instances of UIImage)

@end

.m 文件:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) CGFloat pageWidth;
@property (nonatomic, assign) int currentPage;

@end

@implementation ViewController


-(id)initWithImages:(NSArray*)images{

    self = [super init];

    if(self){

        self.images = images;

    }
    return self;

}

-(void)viewDidLoad{
    [super viewDidLoad];

    [self.scrollView setDelegate:self];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.imageURLsArray.count,
                                               self.scrollView.frame.size.height)];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setShowsVerticalScrollIndicator:NO];

    self.pageWidth = self.scrollView.frame.size.width;


    for(int i = 0; i < self.images.count; i++){

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width*i,
                                                                              self.scrollView.frame.origin.y,
                                                                              self.scrollView.frame.size.width,
                                                                              self.scrollView.frame.size.height)];
        imageView.image = [self.images objectAtIndex:i];
        [self.scrollView addSubview:imageView];

    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = floor(scrollView.contentOffset.x - self.pageWidth/2)/self.pageWidth + 1;
    if(self.currentPage != page){
        NSLog(@"Scrolled to new page: %d", page);
        self.currentPage = page;
//do additional things based on the fact that you have scrolled to a new page (like changing the page number)
    }
 }
@end

【讨论】:

  • 我需要在一个页面中显示 12 张图片,然后向右滚动,然后像我需要的那样加载下一张。我怀疑你的代码不会这样做
  • 只需将视图控制器的初始化程序更改为,而不是将图像数组作为参数来获取自定义视图数组(包含 12 个图像)。我编写的代码仅用于滚动部分。确保将图像加载功能与滚动分开。
【解决方案2】:

使用 UItableview 创建带有两个图像视图的自定义单元格并将该自定义单元格加载到索引路径的单元格,然后创建一个 nsarray 并将图像名称传递到该数组中,然后将该图像转换为数据格式,例如

nsdata *data = [NSdata dataWithcontents of url [array object at index:indexpath.row]]; cell.imageview.image = [uiimage imagewithdata:data]; 为此,您应该知道创建自定义单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多