//
//  STImageVIew.m
//  STPhotoBroeser
//
//  Created by StriEver on 16/3/15.
//  Copyright 漏 2016骞?StriEver. All rights reserved.
//

#import "STImageVIew.h"
#import "UIView+Extension.h"
@interface STImageVIew()<UIGestureRecognizerDelegate>{
    CGFloat _lastScale;//璁板綍鏈€鍚庝竴娆$殑鍥剧墖鏀惧ぇ鍊嶆暟
}
/**鎵嬫満灞忓箷楂樺害涓嶅鐢ㄧ殑鏃跺€?鐢ㄤ簬鏄剧ず瀹屾暣鍥剧墖*/
@property (nonatomic, strong) UIScrollView * scrollView;
/**瀹屾暣鍥剧墖*/
@property (nonatomic, strong) UIImageView * scrollImgV;
/**鐢ㄤ簬鏀惧ぇ 缂╁皬 鍥剧墖鐨剆crollview*/
@property (nonatomic, strong) UIScrollView * scaleScrollView;
/**鐢ㄤ簬鏄剧ず 鏀惧ぇ缂╁皬鐨?鍥剧墖*/
@property (nonatomic, strong) UIImageView * scaleImgV;
@property (nonatomic, assign) BOOL doubleAction;
@end
@implementation STImageVIew
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        UIPinchGestureRecognizer * ges = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scaleImageViewAction:)];
        ges.delegate = self;
        _lastScale = 1.f;
        [self addGestureRecognizer:ges];
        UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleClick:)];
        [self addGestureRecognizer:singleTap];
        
        UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleClick:)];
        doubleTap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:doubleTap];
        [singleTap requireGestureRecognizerToFail:doubleTap];
    }
    return self;
}
//getter
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        [self addSubview:_scrollView];
    }
    return _scrollView;
}
- (UIImageView *)scrollImgV{
    if (!_scrollImgV) {
        _scrollImgV = [[UIImageView alloc]init];
        _scrollImgV.image = self.image;
        [self.scrollView addSubview:_scrollImgV];
    }
    return _scrollImgV;
}
- (UIScrollView *)scaleScrollView{
    if (!_scaleScrollView) {
        _scaleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _scaleScrollView.bounces = NO;
        _scaleScrollView.backgroundColor = [UIColor blackColor];
        _scaleScrollView.contentSize =  self.bounds.size;
        [self addSubview:_scaleScrollView];
    }
    return _scaleScrollView;
}
- (UIImageView *)scaleImgV{
    if (!_scaleImgV) {
        _scaleImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _scaleImgV.image = self.image;
        [self.scaleScrollView addSubview:_scaleImgV];
    }
    return _scaleImgV;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGSize imageSize = self.image.size;
    //鍥剧墖楂樺害澶т簬灞忓箷楂樺害
    if (self.width * (imageSize.height / imageSize.width) > self.height) {
        [self scrollView];
        self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.width * (imageSize.height / imageSize.width));
        self.scrollImgV.center = self.scrollView.center;
        self.scrollImgV.bounds = CGRectMake(0, 0, imageSize.width, self.width * (imageSize.height / imageSize.width));
    }else{
        if (_scrollView)[_scrollView removeFromSuperview];
    }
    
}
#pragma mark ---action
-(void)scaleImageViewAction:(UIPinchGestureRecognizer*)sender {
    
    CGFloat scale = sender.scale;//寰楀埌鐨勬槸褰撳墠鎵嬪娍鏀惧ぇ鍊嶆暟
    NSLog(@"--------%f",scale);
    CGFloat shouldScale = _lastScale + (scale - 1);//鎴戜滑闇€瑕佺煡閬撶殑鏄綋鍓嶆墜鍔跨浉鏀剁缉鐜囧浜庡垰鎵嶆墜鍔跨殑鐩稿鏀剁缉 scale - 1锛岀劧鍚庡姞涓婃渶鍚庝竴娆℃敹缂╃巼锛屼负褰撳墠瑕佸睍绀虹殑鏀剁缉鐜?    [self setScaleImageWithScale:shouldScale];
    sender.scale = 1.0;//鍥剧墖澶у皬鏀瑰彉鍚庤缃墜鍔縮cale涓?
}
- (void)setScaleImageWithScale:(CGFloat)scale{
    //鏈€澶?鍊嶆渶灏?5
    if (scale >=2) {
        scale = 2;
    }else if(scale <=.5){
        scale = .5;
    }
    _lastScale = scale;
    self.scaleImgV.transform = CGAffineTransformMakeScale(scale, scale);
    if (scale > 1) {
        CGFloat imageWidth = self.scaleImgV.width;
        CGFloat imageHeight =  MAX(self.scaleImgV.height, self.frame.size.height);
        [self bringSubviewToFront:self.scaleScrollView];
        self.scaleImgV.center = CGPointMake(imageWidth * 0.5, imageHeight * 0.5);
        self.scaleScrollView.contentSize = CGSizeMake(imageWidth, imageHeight);
        CGPoint offset = self.scaleScrollView.contentOffset;
        offset.x = (imageWidth - self.width)/2.0;
        offset.y = (imageHeight - self.height)/2.0;
        self.scaleScrollView.contentOffset = offset;
    }else{
        self.scaleImgV.center = self.scaleScrollView.center;
        self.scaleScrollView.contentSize = CGSizeZero;
        
    }
}
- (void)singleClick:(UITapGestureRecognizer *)tap{
    if (_delegate &&[_delegate respondsToSelector:@selector(stImageVIewSingleClick:)]) {
        [_delegate stImageVIewSingleClick:self];
    }
}

- (void)doubleClick:(UITapGestureRecognizer *)tap{
    if (_lastScale > 1) {
        _lastScale = 1;
        
    }else{
        _lastScale = 2;
    }
    [UIView animateWithDuration:.5 animations:^{
         [self setScaleImageWithScale:_lastScale];
        
    }completion:^(BOOL finished) {
        if (_lastScale == 1) {
            [self resetView];
        }
    }];
   
}
//褰撹揪鍒板師鍥惧ぇ灏?娓呴櫎 鏀惧ぇ鐨勫浘鐗?鍜宻crollview
- (void)resetView{
    if (!self.scaleScrollView) {
        return;
    }
    self.scaleScrollView.hidden = YES;
    [self.scaleScrollView removeFromSuperview];
    self.scaleScrollView = nil;
    self.scaleImgV = nil;
}
@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com