我创建了一个带有UICollectionView
的图库,并从plist文件中加载了一组图像,当用户触摸每个单元格时,将加载一个全屏图像(与iPhone的照片应用程序相同),所以我遇到了有一个问题,即当照片将全屏时,我无法滚动和查看其他图像,这是我的代码:
然后在PhotosViewerController中:
-(void) creatContentWithNumerOfPage: (NSInteger) numberOfPage andCurrentPage: (NSInteger) currentPage andImageArray:(NSArray *)imageArray {
self.pageControl.numberOfPages = numberOfPage;
self.pageControl.currentPage = currentPage;
[pageScrollView setContentSize:CGSizeMake(self.view.frame.size.width * pageControl.numberOfPages, self.view.frame.size.height)];
pageScrollView.delegate = self;
[self.view addSubview: pageScrollView];
NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:thumbnailViewController.imageArray.count];
for (int i = 0; i <= thumbnailViewController.imageArray.count; i++)
{
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];
imageArray = [picturesDictionary objectForKey:@"PhotosArray"];
fullScreen = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:currentPage]]];
[fullScreen setContentMode:UIViewContentModeScaleAspectFit];
fullScreen.frame = self.view.frame;
[mArray addObject:fullScreen];
[pageScrollView addSubview:fullScreen];
}
}
编辑:(解决方案)
-(void) creatContentWithImages:(NSArray*)imageArray andCurrentPage:(NSInteger)currentPage{
self.pageControl.numberOfPages = imageArray.count;
self.pageControl.currentPage = currentPage;
[self.pageScrollView setContentSize:CGSizeMake(self.pageScrollView.frame.size.width * imageArray.count, self.pageScrollView.frame.size.height)];
for (int i = 0; i < imageArray.count; i++)
{
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];
imageArray = [picturesDictionary objectForKey:@"PhotosArray"];
fullScreen = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
[fullScreen setContentMode:UIViewContentModeScaleAspectFit];
fullScreen.frame = CGRectMake(i*self.pageScrollView.frame.size.width, 0, self.pageScrollView.frame.size.width, self.pageScrollView.frame.size.height);
[self.pageScrollView addSubview:fullScreen];
}
[self scrollToPage:currentPage animated:NO];
}
答案 0 :(得分:2)
您的代码存在很多问题,例如
您在 creatContentWithImages
方法之前致电viewDidLoad
并在此处
我们不能指望self.view.frame
或{的实际值
self.pageScrollView.frame
,您必须在viewDidLoad
之后/之后调用它。
也许是这样的:
[self presentViewController:photosViewController animated:YES completion:^{
[photoViewController creatContentWithImages:images andCurrentPage:row];
}];
在周期中,您始终使用currentPage
代替i
ivar
imageArray
发送到creatContentWithNumerOfPage
方法,但是
出于某种原因,这个数组在这里用新数据覆盖(I [self.view addSubview:
pageScrollView]
- 是否以编程方式创建了UIScrollView?但它在哪个地方发生?我已经改变了它并改进了一些东西,所以让我们看一下(我发布了所有的PhotosViewerController.m文件)
#import "PhotosViewerController.h"
@implementation PhotosViewerController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) creatContentWithImages:(NSArray*)imageArray andCurrentPage:(NSInteger)currentPage{
// I create UIScrollView programmatically, if you want to use xib then just remove lines with RM comment.
// There are two views in xib hierarchy - UIScrollView downward and UIPageView above
self.pageScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; //RM
self.pageScrollView.pagingEnabled = YES; //RM
self.pageControl.numberOfPages = imageArray.count;
self.pageControl.currentPage = currentPage;
[self.pageScrollView setContentSize:CGSizeMake(self.pageScrollView.frame.size.width * imageArray.count, self.pageScrollView.frame.size.height)];
self.pageScrollView.delegate = self;//RM
[self.view insertSubview:self.pageScrollView atIndex:0]; //RM
for (int i = 0; i < imageArray.count; i++) //not <=, just <
{
UIImage *img = [imageArray objectAtIndex:i];// not sure whether imageArray contains UIImage's or NSString, so if here NSString the create UIImage in appropriate manner. I've tested it with UIImage's
UIImageView *fullScreen = [[UIImageView alloc] initWithImage:img];
[fullScreen setContentMode:UIViewContentModeScaleAspectFit];
fullScreen.frame = CGRectMake(i*self.pageScrollView.frame.size.width, 0, self.pageScrollView.frame.size.width, self.pageScrollView.frame.size.height);
[self.pageScrollView addSubview:fullScreen];
}
[self scrollToPage:currentPage animated:NO]; // scroll to corresponding page
}
- (void)scrollViewDidScroll: (UIScrollView *) sView
{
CGFloat offset = self.pageScrollView.contentOffset.x;
CGFloat pageSize = self.pageScrollView.frame.size.width;
int page = floor((offset + (pageSize/2)) / pageSize);
self.pageControl.currentPage = page;
}
- (IBAction)changeThePage:(id)sender
{
NSLog(@"Change");
[self scrollToPage:self.pageControl.currentPage animated:YES];
}
- (void)scrollToPage:(NSInteger)page animated:(BOOL)animated{
CGRect pageRect = CGRectMake(page * self.pageScrollView.frame.size.width, 0, self.pageScrollView.frame.size.width, self.pageScrollView.frame.size.height);
[self.pageScrollView scrollRectToVisible: pageRect animated: animated];
[self.pageControl setCurrentPage:page];
}
@end
如果您还有任何疑问,请告诉我,我会尽力帮助您。