JZAlbumViewController.m 8.21 KB
//
//  JZAlbumViewController.m
//  aoyouHH
//
//  Created by jinzelu on 15/4/27.
//  Copyright (c) 2015年 jinzelu. All rights reserved.
//

#import "JZAlbumViewController.h"
//#import "LocalFileViewController.h"
//#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "PhotoView.h"


#define IS_PAD (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)

@interface JZAlbumViewController ()<UIScrollViewDelegate,PhotoViewDelegate,UINavigationControllerDelegate>
{
    CGFloat lastScale;
    MBProgressHUD *HUD;
    NSMutableArray *_subViewList;
}

@end

@implementation JZAlbumViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //
        _subViewList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    lastScale = 1.0;
    self.view.backgroundColor = [UIColor blackColor];
    
    [self initScrollView];
    
    [self setPicCurrentIndex:self.currentIndex]; 
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    self.navigationController.navigationBarHidden = NO;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    } else {
        self.automaticallyAdjustsScrollViewInsets = YES;
    }
}

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

-(void)initScrollView{

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;

    self.scrollView.contentSize = CGSizeMake(self.imgArr.count*ScreenWidth, 0);
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    //设置放大缩小的最大,最小倍数

    [self.view addSubview:self.scrollView];
    
    for (int i = 0; i < self.imgArr.count; i++) {
        [_subViewList addObject:[NSNull class]];
    }

}

-(void)addLabels{
    self.sliderLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, ScreenHeight-64-49, 60, 30)];
    self.sliderLabel.backgroundColor = [UIColor clearColor];
    self.sliderLabel.textColor = [UIColor whiteColor];
    self.sliderLabel.text = [NSString stringWithFormat:@"%ld/%lu",self.currentIndex+1,(unsigned long)self.imgArr.count];
    [self.view addSubview:self.sliderLabel];
}


-(void)setPicCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    self.scrollView.contentOffset = CGPointMake(ScreenWidth*currentIndex, 0);
    
    [self loadPhote:_currentIndex];
    
    if (_currentIndex == self.imgArr.count-1) {
        [self loadPhote:_currentIndex-1];
    }else if (_currentIndex == 0){
        [self loadPhote:_currentIndex+1];
    }else{
        [self loadPhote:_currentIndex-1];
        [self loadPhote:_currentIndex+1];
    }
    
}

-(void)loadPhote:(NSInteger)index{
    if (index<0 || index >=self.imgArr.count) {
        return;
    }
    
    id currentPhotoView = [_subViewList objectAtIndex:index];
    
    if (![currentPhotoView isKindOfClass:[PhotoView class]]) {
        
        if ([self.imgArr[index] isKindOfClass:[NSString class]]) {
            CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
            PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:[self.imgArr objectAtIndex:index]];
            photoV.delegate = self;
//            [self.scrollView insertSubview:photoV atIndex:0];
            [self.scrollView addSubview:photoV];
            [_subViewList replaceObjectAtIndex:index withObject:photoV];
            
        }
//        else if ([self.imgArr[index] isKindOfClass:[NIMMessage class]]) {
//            NIMMessage *message = self.imgArr[index];
//
//            if ([message.messageObject isKindOfClass:[NIMImageObject class]]) {
//                NIMImageObject *image = message.messageObject;
//
//                CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
//                PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:image.url];
//                photoV.delegate = self;
//                [self.scrollView addSubview:photoV];
//                [_subViewList replaceObjectAtIndex:index withObject:photoV];
//
//            }else if ([message.messageObject isKindOfClass:[NIMVideoObject class]]) {
//                NIMVideoObject *video = message.messageObject;
//
//                CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
//                PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withVideo:video];
//                photoV.delegate = self;
//                [self.scrollView addSubview:photoV];
//                [_subViewList replaceObjectAtIndex:index withObject:photoV];
//
//                NSLog(@"%@", currentPhotoView);
//            }
//
//        }
    
    }else{

    }
    
}


#pragma mark - PhotoViewDelegate
-(void)TapHiddenPhotoView{
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)longPressGesturePhotoView:(UIAlertController *)alertVC
{
    if (IS_PAD) {

        UIPopoverPresentationController *popPresenter = [alertVC
                                                         popoverPresentationController];
        popPresenter.sourceView = [CommonFun sharedInstance].topViewController.view; // 这就是挂靠的对象
        popPresenter.sourceRect = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
        [self presentViewController:alertVC animated:YES completion:nil];

    }else{
        [self presentViewController:alertVC animated:YES completion:nil];
    };
}

-(void)OnTapView{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//手势
-(void)pinGes:(UIPinchGestureRecognizer *)sender{
    if ([sender state] == UIGestureRecognizerStateBegan) {
        lastScale = 1.0;
    }
    CGFloat scale = 1.0 - (lastScale -[sender scale]);
    lastScale = [sender scale];
    self.scrollView.contentSize = CGSizeMake(self.imgArr.count*ScreenWidth, ScreenHeight*lastScale);
    NSLog(@"scale:%f   lastScale:%f",scale,lastScale);
    CATransform3D newTransform = CATransform3DScale(sender.view.layer.transform, scale, scale, 1);
    
    sender.view.layer.transform = newTransform;
    if ([sender state] == UIGestureRecognizerStateEnded) {
        //
    }
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidEndDecelerating");
    int i = scrollView.contentOffset.x/ScreenWidth+1;
    _currentIndex = i;
    [self loadPhote:i-1];
    self.sliderLabel.text = [NSString stringWithFormat:@"%d/%lu",i,(unsigned long)self.imgArr.count];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSLog(@"%f",offsetX);
    
}

// 将要显示控制器
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isSelf = [viewController isKindOfClass:[self class]];
    if(isSelf){
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }else{
        NSLog(@"将要展示的viewController不是自己");
    }
}

@end