如何在iphone中通过可怕的分区滚动在scrollview中加载图像包?
我有一个iPhone应用程序,其中我需要加载大量图像(即大约50个),它有一个小的矩形(略高于缩略图),每行3个,一页4列,我需要水平滚动以加载接下来的12个图像,以此类推。我还需要显示它下面的页码为1,2等如何在iphone中通过可怕的分区滚动在scrollview中加载图像包?,iphone,ios,ipad,uiscrollview,Iphone,Ios,Ipad,Uiscrollview,我有一个iPhone应用程序,其中我需要加载大量图像(即大约50个),它有一个小的矩形(略高于缩略图),每行3个,一页4列,我需要水平滚动以加载接下来的12个图像,以此类推。我还需要显示它下面的页码为1,2等 任何人都可以引导我在滚动视图中实现这一点吗?我真的很混乱,如何将imageview添加到这样的滚动视图中,每个图像视图也有自己的按钮。我解决这个问题的方法如下:(我不会提供很多细节,因为它们取决于您的特殊需求和要求) .h文件: #import <UIKit/UIKit.h>
任何人都可以引导我在滚动视图中实现这一点吗?我真的很混乱,如何将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
#导入
@界面ViewController:UIViewController
@属性(不安全的、非原子的)IBMOutlet UIScrollView*scrollView;//假设scrollView在xib文件中连接
-(id)initWithImages:(NSArray*)images;//可以是您获取图像的任何格式(UIImage、要下载的URL、文件名或自定义视图。这里我们假设数组包含UIImage的实例)
@结束
.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
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)NSArray*图像;
@属性(非原子,赋值)CGFloat pageWidth;
@属性(非原子,赋值)int currentPage;
@结束
@实现视图控制器
-(id)initWithImages:(NSArray*)图像{
self=[super init];
如果(自我){
自我形象=形象;
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
[self.scrollView setDelegate:self];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.imageURLsArray.count,
self.scrollView.frame.size.height)];
[self.scrollView setPaginEnabled:是];
[self.scrollView设置ShowsVerticalScrollIndicator:否];
self.pageWidth=self.scrollView.frame.size.width;
对于(int i=0;i
使用UItableview创建带有两个ImageView的自定义单元格,并将该自定义单元格加载到indexpath的单元格中,然后创建一个nsarray,并将图像名称传递到该数组中,然后将该图像转换为数据格式,以便
nsdata*data=[nsdata dataWithcontents of url[索引处的数组对象:indexath.row]];
cell.imageview.image=[uiimage-imagewithdata:data];
为此,您应该知道如何创建自定义单元格。对于水平表视图,请检查此url。.图像来自何处?来自我的数组。它是从我的捆绑包创建的。要创建视图控制器,将此数组作为其初始值。然后创建自定义视图,以您认为合适的方式显示图像。您可以创建这些自定义视图的数组,并使用我在下面的回答中编写的代码在它们之间提供滚动。希望这是清楚的。我需要在一个页面中显示12个图像,然后向右滚动,然后像我需要的那样加载下一个。我怀疑你的代码不会这样做。只需将视图控制器的初始值更改为,而不是使用数组作为参数获取自定义视图的数组(包含12个图像)。我编写的代码仅用于滚动部分。请确保将图像加载功能与滚动分离。