forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMJPhotoView.m
More file actions
executable file
·214 lines (182 loc) · 6.43 KB
/
Copy pathMJPhotoView.m
File metadata and controls
executable file
·214 lines (182 loc) · 6.43 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// MJZoomingScrollView.m
//
// Created by mj on 13-3-4.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJPhotoView.h"
#import "MJPhoto.h"
#import "MJPhotoLoadingView.h"
#import <QuartzCore/QuartzCore.h>
#import "YLGIFImage.h"
#import "YLImageView.h"
@interface MJPhotoView ()
{
BOOL _zoomByDoubleTap;
YLImageView *_imageView;
MJPhotoLoadingView *_photoLoadingView;
}
@end
@implementation MJPhotoView
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.clipsToBounds = YES;
// 图片
_imageView = [[YLImageView alloc] init];
_imageView.backgroundColor = kColorTableBG;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_imageView];
// 进度条
_photoLoadingView = [[MJPhotoLoadingView alloc] init];
// 属性
self.delegate = self;
// self.showsHorizontalScrollIndicator = NO;
// self.showsVerticalScrollIndicator = NO;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// 监听点击
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.delaysTouchesBegan = YES;
singleTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
}
return self;
}
//设置imageView的图片
- (void)configImageViewWithImage:(UIImage *)image{
_imageView.image = image;
}
#pragma mark - photoSetter
- (void)setPhoto:(MJPhoto *)photo {
_photo = photo;
[self showImage];
}
#pragma mark 显示图片
- (void)showImage
{
[self photoStartLoad];
[self adjustFrame];
}
#pragma mark 开始加载图片
- (void)photoStartLoad
{
if (_photo.image) {
[_photoLoadingView removeFromSuperview];
_imageView.image = _photo.image;
self.scrollEnabled = YES;
} else {
_imageView.image = _photo.placeholder;
self.scrollEnabled = NO;
// 直接显示进度条
[_photoLoadingView showLoading];
[self addSubview:_photoLoadingView];
ESWeakSelf;
ESWeak_(_photoLoadingView);
ESWeak_(_imageView);
[SDWebImageManager.sharedManager downloadImageWithURL:_photo.url options:SDWebImageRetryFailed| SDWebImageLowPriority| SDWebImageHandleCookies progress:^(NSInteger receivedSize, NSInteger expectedSize) {
ESStrong_(_photoLoadingView);
if (receivedSize > kMinProgress) {
__photoLoadingView.progress = (float)receivedSize/expectedSize;
}
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
ESStrongSelf;
ESStrong_(_imageView);
__imageView.image = image;
[_self photoDidFinishLoadWithImage:image];
}];
}
}
#pragma mark 加载完毕
- (void)photoDidFinishLoadWithImage:(UIImage *)image
{
if (image) {
self.scrollEnabled = YES;
_photo.image = image;
[_photoLoadingView removeFromSuperview];
if ([self.photoViewDelegate respondsToSelector:@selector(photoViewImageFinishLoad:)]) {
[self.photoViewDelegate photoViewImageFinishLoad:self];
}
} else {
[self addSubview:_photoLoadingView];
[_photoLoadingView showFailure];
}
// 设置缩放比例
[self adjustFrame];
}
#pragma mark 调整frame
- (void)adjustFrame
{
if (_imageView.image == nil) return;
// 基本尺寸参数
CGFloat boundsWidth = self.bounds.size.width;
CGFloat boundsHeight = self.bounds.size.height;
CGFloat imageWidth = _imageView.image.size.width;
CGFloat imageHeight = _imageView.image.size.height;
// 设置伸缩比例
CGFloat imageScale = boundsWidth / imageWidth;
CGFloat minScale = MIN(1.0, imageScale);
CGFloat maxScale = 2.0;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
maxScale = maxScale / [[UIScreen mainScreen] scale];
}
self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;
self.zoomScale = minScale;
CGRect imageFrame = CGRectMake(0, MAX(0, (boundsHeight- imageHeight*imageScale)/2), boundsWidth, imageHeight *imageScale);
self.contentSize = CGSizeMake(CGRectGetWidth(imageFrame), CGRectGetHeight(imageFrame));
_imageView.frame = imageFrame;
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
if (_zoomByDoubleTap) {
CGFloat insetY = (CGRectGetHeight(self.bounds) - CGRectGetHeight(_imageView.frame))/2;
insetY = MAX(insetY, 0.0);
if (ABS(_imageView.frame.origin.y - insetY) > 0.5) {
[_imageView setY:insetY];
}
}
return _imageView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
_zoomByDoubleTap = NO;
CGFloat insetY = (CGRectGetHeight(self.bounds) - CGRectGetHeight(_imageView.frame))/2;
insetY = MAX(insetY, 0.0);
if (ABS(_imageView.frame.origin.y - insetY) > 0.5) {
[UIView animateWithDuration:0.2 animations:^{
[_imageView setY:insetY];
}];
}
}
#pragma mark - 手势处理
//单击隐藏
- (void)handleSingleTap:(UITapGestureRecognizer *)tap {
// 移除进度条
[_photoLoadingView removeFromSuperview];
// 通知代理
if ([self.photoViewDelegate respondsToSelector:@selector(photoViewSingleTap:)]) {
[self.photoViewDelegate photoViewSingleTap:self];
}
}
//双击放大
- (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
_zoomByDoubleTap = YES;
if (self.zoomScale == self.maximumZoomScale) {
[self setZoomScale:self.minimumZoomScale animated:YES];
} else {
CGPoint touchPoint = [tap locationInView:self];
CGFloat scale = self.maximumZoomScale/ self.zoomScale;
CGRect rectTozoom=CGRectMake(touchPoint.x * scale, touchPoint.y * scale, 1, 1);
[self zoomToRect:rectTozoom animated:YES];
}
}
- (void)dealloc
{
// 取消请求
[_imageView sd_setImageWithURL:[NSURL URLWithString:@"file:///abc"]];
}
@end