//
//  ICBigPhotoViewerCell.m
//  superCleaner
//
//  Created by Mazy on 2020/7/5.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICBigPhotoViewerCell.h"
#import "UIView+HExtension.h"

@interface ICBigPhotoViewerCell () <UIScrollViewDelegate>

@end

@implementation ICBigPhotoViewerCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.scrollView.delegate = self;
    
    self.scrollView.minimumZoomScale = 1;
    self.scrollView.maximumZoomScale = 5.0;
    
    self.bigImageView.userInteractionEnabled = true;
}

- (IBAction)selectedOrNotAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    if (_refreshViewDataClosre) {
        _refreshViewDataClosre(sender.isSelected);
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.bigImageView;
}

//scrollView进行了缩放
//通过方法zoomToRect来缩放时，这个方法只会调用一次，调用时间点在scrollViewWillBeginZooming之后，scrollViewDidEndZooming之前
//使用双指捏合手势缩放中，这个方法会一直调用,缩放中,scrollView的contentSize会变化,即放大缩小
//如果内容宽(高)小于scrollView宽(高),内容以scrollView宽(高)为准居中
//如果内容宽(高)大于scrollView宽(高),内容从x=0(y=0)开始
//如此便达到了,内容任一方向小于父容器时,居中该方向,
//大于父容器时,内容占满该方向
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    UIView *targetView = [self viewForZoomingInScrollView:scrollView];
    BOOL widthIsSmall = targetView.width < scrollView.width;
    BOOL heightIsSmall = targetView.height < scrollView.height;
    
    if (widthIsSmall) {
        targetView.centerX = scrollView.centerX;
    } else {
        targetView.x = 0;
    }
    
    if (heightIsSmall) {
        targetView.centerY = scrollView.centerY;
    } else {
        targetView.y = 0;
    }
}

@end
