GalleryViewController.m 3.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
//
//  GalleryViewController.m
//  vanke
//
//  Created by Z on 16/7/18.
//  Copyright © 2016年 gomore. All rights reserved.
//

#import "ZJBaseFileModel.h"
#import "GalleryCollectionViewCell.h"
#import "GalleryViewController.h"
@interface GalleryViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@end

@implementation GalleryViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.flowLayout.itemSize = CGSizeMake(kWidth, kHeight);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    switch (self.type) {
        case GalleryTypeReadOnly: {
            break;
        }
        case GalleryTypeEdit: {
            UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deletePic)];
            self.navigationItem.rightBarButtonItem = rightBtn;
            break;
        }
    }
}

- (void)deletePic
{
    WS(weakSelf);
    [self alertTitle:@"确认删除这张图片?"
                 msg:nil
            okAction:^(UIAlertAction *action) {
                weakSelf.page = weakSelf.collectionView.contentOffset.x / weakSelf.view.frame.size.width;
                [weakSelf.arrData removeObjectAtIndex:weakSelf.page];
                if (self.arrData.count == 0) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                } else {
                    NSIndexPath *index = [NSIndexPath indexPathForRow:weakSelf.page inSection:0];
                    [weakSelf.collectionView deleteItemsAtIndexPaths:@[ index ]];

                }
                //删除之后的回调,这里的数字是随便传的
                weakSelf.blockDelete(0);

            }
        cancelAction:nil];
}

- (void)viewDidLayoutSubviews
{
    if (self.page < 0) {
        return;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.page inSection:0]
                                atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                        animated:NO];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalleryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];
    [cell configCellWithArray:self.arrData indexPath:indexPath];
    
    self.title = [NSString stringWithFormat:@"(%ld/%.0lu)", indexPath.row + 1, (unsigned long)self.arrData.count];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.title = [NSString stringWithFormat:@"(%.0f/%.0lu)", scrollView.contentOffset.x / kWidth + 1, (unsigned long)self.arrData.count];
    self.page = scrollView.contentOffset.x / self.view.frame.size.width;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end