123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // JXHZoomImageView.m
- // 我的社保
- //
- // Created by shanp on 2020/9/17.
- //
- #import "JXHZoomImageView.h"
- #import "JXHMacro.h"
- #import "JXHSystemShortcut.h"
- #import "JXHLayout.h"
- #import <JXHViewMeasure.h>
- #import <UIImageView+WebCache.h>
- @interface JXHZoomImageView ()<UIScrollViewDelegate> {
-
- CGFloat _maxZoom;
-
- CGFloat _verticalContentInset;
- }
- @end
- @implementation JXHZoomImageView
- - (instancetype)initWithTarget:(id)target action:(SEL)action {
- self = [super init];
- if (!self) return nil;
- [self initViewWithTarget:target action:action];
- [self createImageView];
- return self;
- }
- - (void)initViewWithTarget:(id)target action:(SEL)action {
- self.minimumZoomScale = 1.f;
- self.maximumZoomScale = _maxZoom = 2.f;
- self.showsHorizontalScrollIndicator = false;
- self.showsVerticalScrollIndicator = false;
- self.multipleTouchEnabled = true;
- self.delegate = self;
-
- UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
- [doubleTap setNumberOfTapsRequired:2];
- [self addGestureRecognizer:doubleTap];
-
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
- [singleTap setNumberOfTapsRequired:1];
- [self addGestureRecognizer:singleTap];
-
- //只有当没有检测到doubleTap 或者 检测doubleTap失败,sigleTap才有效
- [singleTap requireGestureRecognizerToFail:doubleTap];
- }
- - (void)createImageView {
- _jxh_imageView = UIImageView.new;
- _jxh_imageView.frame = (CGRect){CGPointZero, (CGSize){jxh_viewWidth(self), jxh_viewHeight(self)}};
- _jxh_imageView.contentMode = UIViewContentModeScaleAspectFit;
- [self addSubview:_jxh_imageView];
- }
- #pragma mark - Action
- -(void)doubleTap:(UIGestureRecognizer *)gestureRecognizer{
- NSInteger newScale = [self zoomScale] == 1 ? _maxZoom : 1;
- [self setZoomScale:newScale animated:true];
- }
- - (void)setImageUrl:(NSURL *)url {
- @weakify(self)
- [_jxh_imageView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
- @strongify(self)
- [self setImage:image];
- }];
- }
- - (void)setImage:(nullable UIImage *)image {
- if (jxh_viewWidth(self) == 0.f) {
- [self.superview layoutSubviews];
- }
- [self setZoomScale:1 animated:false];
- CGSize size;
- if (!image) {
- self.maximumZoomScale = _maxZoom = 2.f;
- _verticalContentInset = 0.f;
- size = (CGSize){jxh_viewWidth(self), jxh_viewHeight(self)};
- } else if (image.size.width / image.size.height >= jxh_viewWidth(self) / jxh_viewHeight(self)) {
- CGFloat height = image.size.height / image.size.width * jxh_viewWidth(self);
- _maxZoom = jxh_viewHeight(self) / height;
- if (_maxZoom < 2) {
- self.maximumZoomScale = _maxZoom = 2.0;
- } else {
- self.maximumZoomScale = _maxZoom * 2.0;
- }
- _verticalContentInset = (jxh_viewHeight(self) - height) / 2;
- size = (CGSize){jxh_viewWidth(self), jxh_viewHeight(self)};
- } else {
- self.maximumZoomScale = _maxZoom = 2.f;
- _verticalContentInset = 0.f;
- size = (CGSize){jxh_viewWidth(self), image.size.height / image.size.width * jxh_viewWidth(self)};
- }
- _jxh_imageView.frame = (CGRect){CGPointZero, size};
- [self setContentSize:_jxh_imageView.frame.size];
- [self setContentInset:UIEdgeInsetsZero];
- _jxh_imageView.image = image;
- }
- #pragma mark - Delegate
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
- return _jxh_imageView;
- }
- - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
- }
- - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
- if (scale > 1) {
- CGFloat height = _jxh_imageView.image.size.height / _jxh_imageView.image.size.width * jxh_viewWidth(_jxh_imageView);
- CGFloat y = 0.f;
- if (height < jxh_screenHeight()) {
- y = (jxh_screenHeight() - height) / 2;
- }
- _jxh_imageView.frame = (CGRect){(CGPoint){0, y}, _jxh_imageView.frame.size};
- scrollView.contentInset = UIEdgeInsetsMake(- _verticalContentInset * scale, 0, - _verticalContentInset * scale, 0);
- } else {
- _jxh_imageView.frame = (CGRect){CGPointZero, _jxh_imageView.frame.size};
- [scrollView setContentInset:UIEdgeInsetsZero];
- }
-
- }
- - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
- CGFloat height = _jxh_imageView.image.size.height / _jxh_imageView.image.size.width * jxh_viewWidth(_jxh_imageView);
- CGFloat y = 0.f;
- if (height < jxh_screenHeight()) {
- y = (jxh_screenHeight() - height) / 2;
- }
- _jxh_imageView.frame = (CGRect){(CGPoint){0, y}, _jxh_imageView.frame.size};
- scrollView.contentInset = UIEdgeInsetsMake(- _verticalContentInset * scrollView.zoomScale, 0, - _verticalContentInset * scrollView.zoomScale, 0);
- }
- //- (CGRect)zoomRectForScale:(NSInteger)scale withCenter:(CGPoint)center {
- //
- // CGRect zoomRect;
- //
- // zoomRect.size.height = [self frame].size.height / scale;
- // zoomRect.size.width = [self frame].size.width / scale;
- //
- // zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0) - (jxh_screenWidth() - _imageView.frame.size.width) / 2;
- // zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0) - (jxh_screenHeight() - _imageView.frame.size.height) / 2;
- // return zoomRect;
- //}
- @end
|