LargePictureViewController.m 6.03 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
//
//  LargePictureViewController.m
//  redstar
//
//  Created by admin on 16/1/8.
//  Copyright © 2016年 ZWF. All rights reserved.
//

#import "LargePictureViewController.h"
#import <UIImageView+WebCache.h>

@interface LargePictureViewController ()

@property float scale_;

@end

@implementation LargePictureViewController

@synthesize imageScrollView;
@synthesize scale_;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    offset = 0.0;
    scale_ = 1.0;
    
    self.view.backgroundColor = [UIColor whiteColor];
admin's avatar
admin committed
31 32
    self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(-20, 0, (kScreenWidth + 40), (kScreenHeight - 20))];
    self.imageScrollView.backgroundColor = [UIColor whiteColor];
33 34 35 36 37 38 39 40 41 42 43 44 45
    self.imageScrollView.scrollEnabled = YES;
    self.imageScrollView.pagingEnabled = YES;
    self.imageScrollView.delegate = self;
    //self.imageScrollView.contentSize = CGSizeMake((kScreenWidth + 40) * _allImageArray.count, (kScreenHeight - 20));
    self.imageScrollView.contentSize = CGSizeMake((kScreenWidth + 40) * _allImageArray.count, 0);

    self.imageScrollView.contentOffset = CGPointMake(self.index * (kScreenWidth + 40), 0);
    
    for (int i = 0; i < _allImageArray.count; i++){
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTap setNumberOfTapsRequired:2];
        
        UIScrollView *s = [[UIScrollView alloc] initWithFrame:CGRectMake((kScreenWidth + 40) * i, 0, (kScreenWidth + 40), (kScreenHeight - 20))];
admin's avatar
admin committed
46
        s.backgroundColor = [UIColor whiteColor];
47 48 49 50 51 52 53 54 55 56 57
        s.contentSize = CGSizeMake((kScreenWidth + 40), 0);

        s.showsHorizontalScrollIndicator = NO;
        s.showsVerticalScrollIndicator = NO;
        s.delegate = self;
        s.minimumZoomScale = 1.0;
        s.maximumZoomScale = 3.0;
        s.tag = i+1;
        [s setZoomScale:1.0];
        
        UIImageView *imageview = [[UIImageView alloc] init];
admin's avatar
admin committed
58 59 60
        
        [imageview setContentMode:UIViewContentModeScaleAspectFit];

61 62 63 64 65
        if (self.pictureSaveMode == PictureLocalMode) {
            NSString *imageName = _allImageArray[i];
            NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
            UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
            imageview.image = image;
admin's avatar
admin committed
66
        } else if (self.pictureSaveMode == PictureNetWorkMode) {
67
            [imageview sd_setImageWithURL:_allImageArray[i] placeholderImage:[UIImage imageNamed:@"default_pic"]];
admin's avatar
admin committed
68 69 70 71 72
        } else {
            NSString *imageName = _allImageArray[i];
            NSString *fullPath = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:_uuid] stringByAppendingPathComponent:imageName];
            UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
            imageview.image = image;
73 74 75 76 77 78
        }
        imageview.frame = CGRectMake(20, 0, kScreenWidth, (kScreenHeight - 20));
        imageview.userInteractionEnabled = YES;
        imageview.tag = i+1;
        [imageview addGestureRecognizer:doubleTap];
        [s addSubview:imageview];
admin's avatar
admin committed
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        
        [self.imageScrollView addSubview:s];
       
    }
    
    [self.view addSubview:self.imageScrollView];
    
}


#pragma mark - ScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.imageScrollView){
        CGFloat x = scrollView.contentOffset.x;
        if (x==offset){
            
        }
        else {
            offset = x;
            for (UIScrollView *s in scrollView.subviews){
                if ([s isKindOfClass:[UIScrollView class]]){
                    [s setZoomScale:1.0];
                    UIImageView *image = [[s subviews] objectAtIndex:0];
                    image.frame = CGRectMake(20, 0, kScreenWidth, (kScreenHeight - 20));
                }
            }
        }
    }
}



-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    UIView *v = [scrollView.subviews objectAtIndex:0];
    if ([v isKindOfClass:[UIImageView class]]){
        if (scrollView.zoomScale<1.0){
            //         v.center = CGPointMake(scrollView.frame.size.width/2.0, scrollView.frame.size.height/2.0);
        }
    }
}

#pragma mark -
- (void)handleDoubleTap:(UIGestureRecognizer *)gesture{
    
    float newScale = [(UIScrollView*)gesture.view.superview zoomScale] * 1.5;
    CGRect zoomRect = [self zoomRectForScale:newScale  inView:(UIScrollView*)gesture.view.superview withCenter:[gesture locationInView:gesture.view]];
    UIView *view = gesture.view.superview;
    if ([view isKindOfClass:[UIScrollView class]]){
        UIScrollView *s = (UIScrollView *)view;
        [s zoomToRect:zoomRect animated:YES];
    }
}

#pragma mark - Utility methods

-(CGRect)zoomRectForScale:(float)scale inView:(UIScrollView*)scrollView withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    zoomRect.size.height = [scrollView frame].size.height / scale;
    zoomRect.size.width  = [scrollView frame].size.width  / scale;
    
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}

-(CGRect)resizeImageSize:(CGRect)rect{
    CGRect newRect;
    
    CGSize newSize;
    CGPoint newOri;
    
    CGSize oldSize = rect.size;
    if (oldSize.width>=kScreenWidth || oldSize.height>=(kScreenHeight - 20)){
        float scale = (oldSize.width/kScreenWidth>oldSize.height/(kScreenHeight - 20)?oldSize.width/kScreenWidth:oldSize.height/(kScreenHeight - 20));
        newSize.width = oldSize.width/scale;
        newSize.height = oldSize.height/scale;
    }
    else {
        newSize = oldSize;
    }
    newOri.x = (kScreenWidth-newSize.width)/2.0;
    newOri.y = ((kScreenHeight - 20)-newSize.height)/2.0;
    
    newRect.size = newSize;
    newRect.origin = newOri;
    
    return newRect;
}

@end