iOS - 从plist数据滚动图像

时间:2012-11-29 13:23:31

标签: iphone objective-c ios xcode ipad

我创建了一个带有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];

}

1 个答案:

答案 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?但它在哪个地方发生?
  • 当您使用框架操作时,您应该使用UIScrollView框架, 不是UIView,如果你的卷轴大小与UIView不同那么 设计也将被打破。

我已经改变了它并改进了一些东西,所以让我们看一下(我发布了所有的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

如果您还有任何疑问,请告诉我,我会尽力帮助您。