gpt4 book ai didi

iphone - iOS - 从 plist 数据滚动图像

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:13 25 4
gpt4 key购买 nike

我用 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];

}

最佳答案

你的代码有很多问题,例如

  • 你调用creatContentWithImages 之前 viewDidLoad方法和在这我们不能期望 self.view.frame 的实际值或者 self.pageScrollView.frame , 你必须在 viewDidLoad 之后/之后调用它.可能是这样的:

    [self presentViewController:photosViewController animated:YES completion:^{
    [photoViewController creatContentWithImages:images andCurrentPage:row];
    }];
  • 在循环中你总是使用 currentPage而不是 i伊娃

  • 你发送imageArray进入creatContentWithNumerOfPage方法但是
    出于某种原因,这个数组在这里被新数据覆盖(I
    假设使用相同的数据)
  • 也不清楚你为什么执行 [self.view addSubview:<br/>
    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

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

关于iphone - iOS - 从 plist 数据滚动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13626813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com