collection实现电子相册预览可以放到效果

来源:互联网 发布:苍南法院拍卖淘宝网 编辑:程序博客网 时间:2024/05/27 09:46

转自:https://github.com/fuzongjian/FZJPhotosFrameWork


#import "FZJBigPhotoController.h"

#import "FZJBigPhotoCell.h"

/**

 * 宏定义间距

 */

#define margin 30


@interface FZJBigPhotoController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>

/**

 *  大图展示以供选择

 */

@property(nonatomic,strong)UICollectionView * bigCollect;

/**

 *  展示当前选择照片的动态

 */

@property(nonatomic,strong)UILabel * middle;

/**

 *  导航栏右侧的按钮

 */

@property(nonatomic,strong)UIButtonExt * selectStatus;

/**

 *  中间的标题

 */

@property(nonatomic,strong)UILabel * titleLable;



@end


@implementation FZJBigPhotoController


-(void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:YES];

    [self.bigCollectsetContentOffset:CGPointMake((self.fetchResult.count - 1 - self.clickNum) * (SCREEN_WIDTH +margin), 0)];

}


-(void)viewDidLoad{

    [superviewDidLoad];

    [selfconfigBigPhotoControllerUI];

}

#pragma mark --

#pragma mark 初始化UI

-(void)configBigPhotoControllerUI{

    

    self.view.backgroundColor = [UIColorwhiteColor];

    self.automaticallyAdjustsScrollViewInsets =NO;

    

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayoutalloc]init];

    flowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;

    flowLayout.minimumLineSpacing =margin;

    flowLayout.sectionInset =UIEdgeInsetsMake(0,margin/2,0, margin/2);

    flowLayout.itemSize =self.view.size;

    

    _bigCollect = [[UICollectionViewalloc] initWithFrame:CGRectMake(-margin/2,0, SCREEN_WIDTH +margin, SCREEN_HEIGHT)collectionViewLayout:flowLayout];

    [_bigCollectregisterNib:[UINibnibWithNibName:@"FZJBigPhotoCell"bundle:nil]forCellWithReuseIdentifier:@"BigPhotoCell"];

     _bigCollect.pagingEnabled =YES;

    _bigCollect.dataSource =self;

    _bigCollect.delegate =self;

    [self.viewaddSubview:_bigCollect];

    

    UIButtonExt * quit = [UIButtonExtbuttonWithType:UIButtonTypeCustom];

    [quit setBackgroundImage:[UIImageimageNamed:@"No"]forState:UIControlStateNormal];

    [quit setBackgroundImage:[UIImageimageNamed:@"AssetsPickerChecked"]forState:UIControlStateSelected];

    quit.frame =CGRectMake(0,0, 30,30);

    [quit addTarget:selfaction:@selector(selectStatus:)forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:quit];

    _selectStatus = quit;

    

    

    UILabel * titleLable = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 100,44)];

    titleLable.text = [NSStringstringWithFormat:@"%d/%d",(int)self.clickNum,(int)self.fetchResult.count];

    _titleLable = titleLable;

    titleLable.textAlignment =NSTextAlignmentCenter;

    self.navigationItem.titleView = titleLable;

    

}


#pragma mark--

#pragma mark 数据请求


#pragma mark--

#pragma mark 数据加载


#pragma mark--

#pragma mark 事件



#pragma mark ---  大图浏览对图片的增删改查


-(void)selectStatus:(UIButtonExt *)select{

    select.selected = !select.selected;

    if (select.selected) {

        if (self.ChooseArr.count == self.addNum) {

            [SVProgressHUDshowErrorWithStatus:[NSStringstringWithFormat:@"本次最多选择%d",(int)self.addNum]];

            select.selected =NO;

        }else{

            FZJPhotoModel * model = [[FZJPhotoModelalloc]init];

            model.asset =self.fetchResult[select.index];

            model.imageName = [self.fetchResult[select.index]valueForKey:@"filename"];

            [self.ChooseArraddObject:model];

        }

    }else{

        for (FZJPhotoModel * modelin self.ChooseArr) {

            if ([model.imageNameisEqualToString:[self.fetchResult[select.index]valueForKey:@"filename"]]) {

                [self.ChooseArrremoveObject:model];

                break;

            }

        }

    }

}

#pragma mark ---  重写父类返回按钮点击事件


-(void)SuperBackBtnClicked{

    

    if (self.chooseState) {

        if (self.rootBlock) {

            self.rootBlock(self.ChooseArr);

        }

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }else{

        if (self.returnBlock &&self.ChooseArr.count) {

            self.returnBlock(self.ChooseArr);

        }

         [self.navigationControllerpopViewControllerAnimated:YES];

    }


}

-(void)returnBack:(returnBackPhotoArr)block{

    self.returnBlock = block;

}

-(void)returnToRoot:(returnBackPhotoArr)rootBlock{

    self.rootBlock = rootBlock;

}

#pragma mark--

#pragma mark  代理

#pragma mark --- collectionView 代理

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    returnself.fetchResult.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    FZJBigPhotoCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"BigPhotoCell"forIndexPath:indexPath];

    cell.ImageView.image =nil;

    self.selectStatus.selected =NO;

    self.selectStatus.index = indexPath.row;

    CGFloat scale = [UIScreenmainScreen].scale;

    PHAsset * asset =self.fetchResult[indexPath.row];

    [cell showIndicator];

    [[FZJPhotoTooldefaultFZJPhotoTool] getImageByAsset:assetmakeSize:CGSizeMake(asset.pixelWidth * scale, asset.pixelHeight * scale) makeResizeMode:PHImageRequestOptionsResizeModeNonecompletion:^(UIImage *AssetImage) {

        cell.ImageView.image = AssetImage;

        [cell hideIndicator];

    }];

    for (FZJPhotoModel * modelin self.ChooseArr) {

        if ([model.imageNameisEqualToString:[self.fetchResult[indexPath.row]valueForKey:@"filename"]]) {

            self.selectStatus.selected =YES;

        }

    }

    cell.ScrollView.delegate =self;

    [selfaddGestureTapToScrollView:cell.ScrollView];

    return cell;

}

#pragma mark --- ScrollView 代理

/**

 *  动态改变图片展示的状态

 *

 *  @param scrollView 当前的_bigCollect

 */

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView == (UIScrollView *)_bigCollect) {//UICollectionView是继承于UIScrollView

        CGFloat current = scrollView.contentOffset.x / (SCREEN_WIDTH + margin) + 1;

        _titleLable.text = [NSStringstringWithFormat:@"%.f/%d",current,(int)self.fetchResult.count];

    }

}

/**

 *  即将出现的不被方法或者缩小的视图

 *

 *  @param cell           将要出现的Cell

 *  @param indexPath      cell在数据源中得位置

 */

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

    FZJBigPhotoCell * WillCell = (FZJBigPhotoCell *)cell;

    WillCell.ScrollView.zoomScale =1;

}

/**

 *  返回的是图片的视图

 *

 *  @param scrollView 当前的scrollView

 *

 *  @return 返回一个放大或缩小的视图

 */

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return scrollView.subviews[0];

}

#pragma mark ---  scrollView 添加手势

-(void)addGestureTapToScrollView:(UIScrollView *)scrollView{

    UITapGestureRecognizer * singleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(singleTapOnScrollView:)];

    singleTap.numberOfTapsRequired =1;

    [scrollView addGestureRecognizer:singleTap];

    

    UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(doubleTapOnScrollView:)];

    doubleTap.numberOfTapsRequired =2;

    [scrollView addGestureRecognizer:doubleTap];

}

/**

 *  隐藏导航栏和NavgationBar

 *

 *  @param singleTap 单击

 */

-(void)singleTapOnScrollView:(UITapGestureRecognizer *)singleTap{

    if (self.navigationController.navigationBar.isHidden) {

        [selfshowNavBarAndStatusBar];

    }else{

        [selfhideNavBarAndStatusBar];

    }

}

#pragma mark ---  隐藏或者显示导航栏

-(void)showNavBarAndStatusBar{

    self.navigationController.navigationBar.hidden = NO;

    [UIApplicationsharedApplication].statusBarHidden =NO;

}

-(void)hideNavBarAndStatusBar{

    self.navigationController.navigationBar.hidden = YES;

    [UIApplicationsharedApplication].statusBarHidden =YES;

}

/**

 *  放大缩小

 *

 *  @param doubleTap 双击

 */

-(void)doubleTapOnScrollView:(UITapGestureRecognizer *)doubleTap{

    

    UIScrollView * scrollView = (UIScrollView *)doubleTap.view;

    CGFloat scale =1;

    if (scrollView.zoomScale !=3) {

        scale = 3;

    }else{

        scale = 1;

    }

    [selfCGRectForScale:scale WithCenter:[doubleTaplocationInView:doubleTap.view]ScrollView:scrollView Completion:^(CGRect Rect) {

        [scrollView zoomToRect:Rectanimated:YES];

    }];

}

-(void)CGRectForScale:(CGFloat)scale WithCenter:(CGPoint)center ScrollView:(UIScrollView *)scrollView Completion:(void(^)(CGRect Rect))completion{

    CGRect Rect;

    Rect.size.height = scrollView.frame.size.height / scale;

    Rect.size.width  = scrollView.frame.size.width  / scale;

    Rect.origin.x    = center.x - (Rect.size.width  /2.0);

    Rect.origin.y    = center.y - (Rect.size.height /2.0);

    completion(Rect);

}


0 0
原创粉丝点击