关于Collectionview场景的建议

时间:2015-01-17 14:07:00

标签: ios uicollectionview

需要一些关于如何使用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");
                }];
            }
        }];
    }
}

}

2 个答案:

答案 0 :(得分:0)

此框架已具备此类功能,因此您可以查看源代码以了解其工作原理......

https://github.com/mwaterfall/MWPhotoBrowser

答案 1 :(得分:0)

我希望视图是viewController的一部分,而不仅仅是你函数的一部分。然后,让viewController管理图像的滑动,图像和位置。当您的collectionView被点击时,在viewController中设置位置,以便在捕获滑动时您可以增加一个并使用新图像更新您的视图。请告诉我,如果不够清楚,我可以尝试澄清