【发布时间】:2015-01-17 14:07:00
【问题描述】:
需要一些关于如何使用 Collectionview 处理场景的建议。简而言之,该应用程序有一个显示图像的 CV,您可以在其中点击带有图像缩略图的单元格,然后它将显示该图像的全屏视图。我通过在 didSelectItemAtIndexPath 中实例化一个新的 UIView(不是来自情节提要)来实现这一点。因此,来自单元格的图像的全屏视图只是通过点击单元格触发的新 UIView,我将视图的图像设置为与单元格的图像相同......足够简单。全屏视图还有一个与每个图像相关的按钮。点击全屏图像会关闭图像并返回到 CV。所有这些都完美无缺。
但是,我刚刚意识到我还希望能够在全屏模式下滑动浏览所有图像......基本上与 iOS 相册的工作方式非常相似。通过向 didSelectItemAtIndexPath 添加滑动手势并将动作选择器设置为处理滑动的方法,我能够非常快速地编写一些代码来执行此操作,这很有效。但是,这样做的结果实际上只是更改了选择(点击)的原始单元格的图像。因此,在全屏模式下滑动图像时,我无法跟踪所选单元格。
所以我需要有关如何处理此问题的建议。我知道必须有这样的例子,但我找不到任何例子。有人对我应该如何实施这个有任何建议吗?谢谢!
来自 didSelectItemAtIndexPath 的代码...
self.fullScreenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width-10, self.view.bounds.size.height-15)];
self.fullScreenImage.contentMode = UIViewContentModeScaleAspectFit;
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.fullScreenImage addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.fullScreenImage addGestureRecognizer:leftSwipe];
if (!self.isFullScreen) {
self.fullScreenImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
__weak BaseViewController *weakSelf = self;
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
NSLog(@"Starting animiation!");
weakSelf.view.backgroundColor = [UIColor blackColor];
weakSelf.myCollectionView.backgroundColor = [UIColor blackColor];
weakSelf.fullScreenImage.center = self.view.center;
weakSelf.fullScreenImage.backgroundColor = [UIColor blackColor];
weakSelf.fullScreenImage.image = [UIImage imageWithContentsOfFile:coffeeImageData.imageURL.path];
weakSelf.fullScreenImage.transform = CGAffineTransformIdentity; // zoom in effect
[weakSelf.view addSubview:self.fullScreenImage];
[weakSelf.fullScreenImage addSubview:likeButton]; // add the button to the view
}completion:^(BOOL finished){
if (finished) {
NSLog(@"Animation finished!");
weakSelf.isFullScreen = YES;
}
}];
return;
}
处理来自...的滑动手势 - (void)handleLeftSwipe:(UISwipeGestureRecognizer *)sender {
// make sure indexForSwiping is not > than size of array
if (self.indexForSwiping != [self.imageLoadManager.coffeeImageDataArray count]-1) {
self.indexForSwiping += 1;
NSString *cacheKey = self.allCacheKeys[self.indexForSwiping];
if (cacheKey) {
[self.imageCache queryDiskCacheForKey:cacheKey done:^(UIImage *image, SDImageCacheType cacheType) {
if (image) {
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.fullScreenImage.image = image;
} completion:^(BOOL finished) {
NSLog(@"swiping");
}];
}
}];
}
}
}
【问题讨论】:
标签: ios uicollectionview