如何在iphone中使用horrizondal滚动在滚动视图中加载图像包?

时间:2013-03-01 06:17:27

标签: iphone ios ipad uiscrollview

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

任何人都可以指导我在正确的方向上实现滚动视图吗?我真的很乱,如何将imageview添加到这样的滚动视图中,每个图像视图也都有自己的按钮。

2 个答案:

答案 0 :(得分:0)

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

.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

答案 1 :(得分:0)

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

nsdata * data = [url的NSdata dataWithcontents [索引处的数组对象:indexpath.row]]; cell.imageview.image = [uiimage imagewithdata:data];   为此你应该知道创建自定义单元格。