繁体   English   中英

如何在iPhone中使用水平滚动在scrollview中加载图像包?

[英]How to load bundle of images in a scrollview with horrizondal scrolling in iphone?

我有一个iPhone应用程序,需要加载大量图像(即50张左右),该图像有一个小矩形(略高于缩略图),每行包含3个,一页中有4列,并且我需要水平滚动加载接下来的12张图像,依此类推。 我还需要显示它下的页码为1,2等。 像所附图片一样

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

我解决此问题的方法如下:(由于它们取决于您的特定需求和要求,因此我将不提供许多详细信息)。

.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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM