Objective c 在集合视图和分页详细视图之间转换的正确方法
目前我有一个uicollection视图,它在用户照片库中显示特定的相册 在我的mainView.m中,我收集了以下图片:Objective c 在集合视图和分页详细视图之间转换的正确方法,objective-c,alassetslibrary,Objective C,Alassetslibrary,目前我有一个uicollection视图,它在用户照片库中显示特定的相册 在我的mainView.m中,我收集了以下图片: + (ALAssetsLibrary *)defaultAssetsLibrary { static dispatch_once_t pred = 0; static ALAssetsLibrary *library = nil; dispatch_once(&pred, ^{ library = [[ALAssetsLibr
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
- (void)beginLoadingPhotoInfo {
...
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(@"Probs");}
];
}
将它们(缩略图版本)全部加载到集合视图中,并确保所有操作正常
当用户选择一张照片时,我称之为prepareToSegue方法:(仍在mainView.m中)
目前,我正在发送一个包含照片信息的阵列,并尝试滚动到阵列中的位置
我在这里找到了用于水平分页的资源:
它允许使用集合视图进行分页。我写了一个detailViewController类
问题是。我应该如何连接这两者 想法1:让我的mainView发送一个代表所选照片的整数,然后detailViewController将加载该整数并开始延迟加载照片 想法2:以某种方式预加载一些全屏照片,然后发送带有阵列中斑点的整数 想法3:将数字和我的数组对象都发送到detailViewController,这样我就不必再次枚举资产库 这些都是正确的方法吗?还是我完全错过了这个想法
编辑: 我的detail controller中有一个启用分页的uicollectionview流布局。 这是我设置布局的方法:
- (void) setCollectionView {
[self.collectionView registerClass:[DetailViewCell class] forCellWithReuseIdentifier:@"detailViewCell"];
//Flow Layout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
CGFloat pageWidth = self.collectionView.frame.size.width;
NSInteger num = _photosArrayIndex + 1;
CGPoint scrollTo = CGPointMake(pageWidth * num, 0);
NSLog(@"scroll to: %@", NSStringFromCGPoint(scrollTo));
[self.collectionView setContentOffset:scrollTo];
}
它应该做的是从我的主视图中获取值并移动到该图像。不幸的是,事实并非如此。我不知道为什么,而且我觉得有更好的方法。这看起来有点老土
如何更好地连接两个更好的控制器,以及加载照片的正确方式是什么/如何获取照片(在全尺寸细节视图中),当照片在网格布局中时,我正在使用
感谢您的帮助。好的,这有三个部分 首先是显示照片库的
UICollectionViewController
子类(UIImage
)。
第二个是UIPageViewController
子类,用于管理每个PhotoViewController
的左右滑动。
第三个是显示单个照片的UIViewController
子类(PhotoViewController
)
故事板看起来像这样
左边是<代码> UICollectionViewController < /代码>,中间有“<代码> UIPageViewController < /代码>的分段。右侧是一个
UIViewController
,它在属性窗格中设置了一个标识符
(注意,没有与此相关的分段)
PhotoViewController的标识符
在PhotoPageViewController
中,我有一个自定义对象
在属性窗格中设置了类类型PhotoPageModelController
。。。这是作为PhotoPageViewController
的数据源连接的
这几乎是所有的故事板设置所需的
因此,首先要设置的是PhotoPageModelController
。这是PhotoPageViewController
的数据源,因此将分配UIViewController
的子类,以便PhotoPageViewController
可以显示它们
模型控制器
PhotoPageModelController.h
@class PhotoViewController;
@interface PhotoPageModelController : NSObject <UIPageViewControllerDataSource>
// this is the array of the photos. Either an array of UIImages or objects containing
// them or something. My personal project had an array of photoIDs that I could use to
// pull the photos out of Core Data.
// In this example the array will contain instances of UIImage.
@property (nonatomic, strong) NSArray *photos;
- (PhotoViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard;
- (NSUInteger)indexOfViewController:(PhotoViewController *)controller;
@end
#import <Foundation/Foundation.h>
@interface PhotoPageViewController : UIPageViewController
@property (nonatomic, strong) NSArray *photos;
@property (nonatomic) NSUInteger initialIndex;
@end
嗯。这就是照片页面模型控制器
页面视图控制器
PhotoPageViewController的下一步
PhotoPageViewController.h
@class PhotoViewController;
@interface PhotoPageModelController : NSObject <UIPageViewControllerDataSource>
// this is the array of the photos. Either an array of UIImages or objects containing
// them or something. My personal project had an array of photoIDs that I could use to
// pull the photos out of Core Data.
// In this example the array will contain instances of UIImage.
@property (nonatomic, strong) NSArray *photos;
- (PhotoViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard;
- (NSUInteger)indexOfViewController:(PhotoViewController *)controller;
@end
#import <Foundation/Foundation.h>
@interface PhotoPageViewController : UIPageViewController
@property (nonatomic, strong) NSArray *photos;
@property (nonatomic) NSUInteger initialIndex;
@end
照片视图控制器
接下来是将显示实际照片的视图控制器
它所需要的只是一个名为UIImage
的photo
类型的属性,然后是一个UIImageView
来放置它。我将把这件事留给你,因为你可以用许多不同的方法来做
我把一个可缩放的UIScrollView
放在我的屏幕上,这样用户就可以对照片进行缩放了。我还得到了一些额外的信息,如照片拍摄者的姓名和拍摄日期等。。。按你喜欢的方式设置
集合视图序列
最后一部分(最后)是从集合视图到页面视图控制器
这是在prepareforsgue
中完成的
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoSegue"]) {
PhotoPageViewController *controller = segue.destinationViewController;
NSIndexPath *selectedIndex = [self.collectionView indexPathsForSelectedItems][0];
// The PageViewController doesn't need anything except the index to start on...
// i.e. the index of the photo that the user just selected.
controller.initialIndex = (NSUInteger)selectedIndex.item;
// ...and the array of photos it will be displaying.
controller.photos = self.photos;
// Everything else is done by the PageViewController.
}
}
我已经在我最新的应用程序中做到了这一点。当我到达我的计算机时将提供代码。谢谢,期待看到示例。好的,完成。有很多,但我已经按班级分类了。每个班级有一项工作(保持整洁)。如果您对此有任何问题,请告诉我。哇,回答得很好,但我有一个问题,它似乎在PhotoPageViewController.m处中断,错误为-[\uu NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]插入零对象'。有什么想法吗?我目前正试图解决这个问题。同样在同一个文件中,我假设self.event部分只是您项目中的额外代码。在您的photopagemodel控制器中,您的方法viewControllerAtIndex在.h和.m之间存在差异。仔细想想,尝试插入错误可能是我的错。让我查一查。你是说排版索引和索引1?修正了。如果没有,是哪一个?听起来viewDidLoad方法中的initialViewController对象是nil。情节提要中PhotoViewController对象上的标识符可能有问题。确保设置了情节提要ID。如果这不起作用,则单步执行viewControllerAtIndex方法。确保它工作正常。另外,您是否将情节提要中的
数据源
对象连接到PhotoPageViewController
的模型控制器
属性?我说得不恰当。您必须将数据源
以及属性连接到该对象。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoSegue"]) {
PhotoPageViewController *controller = segue.destinationViewController;
NSIndexPath *selectedIndex = [self.collectionView indexPathsForSelectedItems][0];
// The PageViewController doesn't need anything except the index to start on...
// i.e. the index of the photo that the user just selected.
controller.initialIndex = (NSUInteger)selectedIndex.item;
// ...and the array of photos it will be displaying.
controller.photos = self.photos;
// Everything else is done by the PageViewController.
}
}