YWWebScrollView.m
10.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// YWWebScrollView.m
// SmallLook
//
// Created by NeiQuan on 16/6/24.
// Copyright © 2016年 余伟. All rights reserved.
//
#import "YWWebScrollView.h"
#import "UIImageView+WebCache.h"
@interface YWWebScrollView ()<UIScrollViewDelegate> {
UIScrollView *_scrollView;
NSInteger m_iCurrentPage;
}
@end
@implementation YWWebScrollView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor blackColor]];
m_iCurrentPage = 0;
}
return self;
}
#pragma mark --private method--初始化scrollView
-(void)setImageUrls:(NSMutableArray *)imageUrls{
_imageUrls = imageUrls;
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.tag = 5000;
scrollView.delegate = self;
scrollView.frame = self.frame;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*self.imageUrls.count, 0);
scrollView.pagingEnabled = YES;
for (NSInteger i = 0; i < self.imageUrls.count; i++) {
YWWebSmallScrollView *view = [[YWWebSmallScrollView alloc] initWithFrame: CGRectMake(i*scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
view.tag = 100+i;
[view StartLoadImageWithUrl:_imageUrls[i]];
view.cancleShow = ^{
[self clickImageView];//点击图片移除视图
};
[scrollView addSubview:view];
}
[self addSubview:scrollView];
_scrollView = scrollView;
scrollView.contentOffset = CGPointMake(_ScrollViewTag*scrollView.frame.size.width, 0);
}
-(void)clickImageView {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 0.1;
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
});
}
#pragma mark -- UIScrollViewDelegate method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger iCurrentPage = roundf(scrollView.contentOffset.x / scrollView.frame.size.width);
if (iCurrentPage != m_iCurrentPage) {
YWWebSmallScrollView *pZoomView = (YWWebSmallScrollView *)[scrollView viewWithTag:100 + m_iCurrentPage];
[pZoomView RestoreViewScale];
m_iCurrentPage = iCurrentPage;
}
}
@end
#pragma mark - YWWebSmallScrollView
@interface YWWebSmallScrollView()<UIScrollViewDelegate,UIActionSheetDelegate> {
UIImageView *m_pImageView;
UIActivityIndicatorView *m_pActivityIndicatorV;
}
@end
@implementation YWWebSmallScrollView
- (instancetype)initWithFrame:(CGRect)frame {
if ([super initWithFrame:frame]) {
self.backgroundColor = [UIColor blackColor];
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.minimumZoomScale = 1.0f;
self.maximumZoomScale = 3.0f;
self.showsVerticalScrollIndicator = YES;
self.showsHorizontalScrollIndicator = YES;
self.directionalLockEnabled = YES;
self.delegate = self;
//添加单击取消事件
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CancelReviewImage)];
[self addGestureRecognizer:singleTap];
//添加双击事件
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zoomInOrOut:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
//约束单击和双击不能同时生效
[singleTap requireGestureRecognizerToFail:doubleTap];
//添加长按保存图片事件
UILongPressGestureRecognizer *pSaveImage = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(SaveImageToAlbum:)];
[self addGestureRecognizer:pSaveImage];
//添加图片加载指示器
m_pActivityIndicatorV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
m_pActivityIndicatorV.frame = CGRectMake(0, 0, 0 , 0);
m_pActivityIndicatorV.center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
[self addSubview:m_pActivityIndicatorV];
m_pImageView=[[UIImageView alloc] init];
m_pImageView.contentMode=UIViewContentModeScaleAspectFill;
m_pImageView.clipsToBounds=YES;
[self addSubview:m_pImageView];
}
return self;
}
- (void)setFrame:(CGRect)frame {
CGRect fScreenBounds = [[UIScreen mainScreen] bounds];
CGRect fFrame = CGRectMake(frame.origin.x, frame.origin.y, fScreenBounds.size.width, fScreenBounds.size.height);
[super setFrame:fFrame];
}
- (void)RestoreViewScale {
if (self.zoomScale != 1.0f) {
[self setZoomScale:1.0f animated:NO];
}
}
- (void)StartLoadImageWithUrl:(NSString *)url {
if ([url isEqualToString:@""] || url == nil)
return;
[m_pActivityIndicatorV startAnimating];
__weak typeof(self) weakSelf = self;
__block typeof(m_pActivityIndicatorV) pActivityIndicatorV = m_pActivityIndicatorV;
__weak typeof(m_pImageView) weakImageView = m_pImageView;
[m_pImageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
weakImageView.image = image;
[weakSelf RestImageViewFrame];
[pActivityIndicatorV stopAnimating];
}];
}
#pragma mark -- private method
- (void)RestImageViewFrame {
//等比例缩放
if (!m_pImageView)return;
CGSize size = (m_pImageView.image) ? m_pImageView.image.size : m_pImageView.frame.size;
CGFloat fWidth = 0.0,fHeight = 0.0;
if ((size.width >= self.frame.size.width || size.height >= self.frame.size.height) && (size.height / size.width <= 2.0f && size.width / size.height <= 2.0f)) {
//设置图片的高与屏幕相等
if (size.height * (self.frame.size.width / size.width) > self.frame.size.height) {
fHeight = self.frame.size.height;
fWidth = size.width * fHeight / size.height;
} else {
fWidth = self.frame.size.width;
fHeight = fWidth * size.height / size.width;
}
} else {
fWidth = size.width;
fHeight = size.height;
if (size.height / size.width > 2.0f) {
if (size.width > self.frame.size.width / 3.0f) {
fWidth = self.frame.size.width / 3.0f;
fHeight = fWidth * size.height / size.width;
}
} else if (size.width / size.height > 2.0f) {
if (size.height > self.frame.size.height / 3.0f) {
fHeight = self.frame.size.height / 3.0f;
fWidth = size.width * fHeight / size.height;
}
}
}
if (size.height / size.width > 2.0f) {
CGFloat yMargin = fHeight > self.frame.size.height ? 0.0f : (self.frame.size.height - fHeight) / 2.0f;
m_pImageView.frame = CGRectMake((self.frame.size.width - fWidth) / 2.0f, yMargin, fWidth, fHeight);
if (yMargin == 0.0f) {
[self setZoomScale:3.0f animated:NO];
[self setZoomScale:1.0f animated:NO];
[self setContentOffset:CGPointMake(self.contentOffset.x, 0.0f)];
}
} else if (size.width / size.height > 2.0f) {
CGFloat xMargin = fWidth > self.frame.size.width ? 0.0f : (self.frame.size.width - fWidth) / 2.0f;
m_pImageView.frame = CGRectMake(xMargin, (self.frame.size.height - fHeight) / 2.0f, fWidth, fHeight);
if (xMargin == 0.0f) {
[self setZoomScale:3.0f animated:NO];
[self setZoomScale:1.0f animated:NO];
[self setContentOffset:CGPointMake(0.0f, self.contentOffset.y)];
}
} else {
m_pImageView.frame = CGRectMake((self.frame.size.width - fWidth) / 2.0f, (self.frame.size.height - fHeight) / 2.0f, fWidth, fHeight);
}
}
#pragma mark -- UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return m_pImageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGFloat Ws = self.frame.size.width - self.contentInset.left - self.contentInset.right;
CGFloat Hs = self.frame.size.height - self.contentInset.top - self.contentInset.bottom;
CGFloat W = m_pImageView.frame.size.width;
CGFloat H = m_pImageView.frame.size.height;
CGRect rct = m_pImageView.frame;
rct.origin.x = MAX((Ws-W)/2, 0);
rct.origin.y = MAX((Hs-H)/2, 0);
m_pImageView.frame = rct;
}
#pragma mark -- long press gesture method
- (void)SaveImageToAlbum:(UIGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
UIActionSheet *pActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存图片到相册", nil];
[pActionSheet showInView:self];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != actionSheet.cancelButtonIndex) {
//保存图片到相册
UIImageWriteToSavedPhotosAlbum(m_pImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error != NULL){
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDictionary objectForKey:@"CFBundleName"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"保存图片被阻止了" message:[NSString stringWithFormat:@"请到系统->“设置”->“隐私”->“照片”中开启“%@”访问权限",appName] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已保存至照片库"delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] ;
[alertView show];
}
}
#pragma mark -- single tap gesture method
- (void)CancelReviewImage {
self.backgroundColor = [UIColor clearColor];
if (self.cancleShow) {
self.cancleShow();
}
}
#pragma mark -- double tap gesture method
- (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture {
if (self.zoomScale == self.minimumZoomScale) {
CGPoint point = [tapGesture locationInView:self];
[self zoomToRect:CGRectMake(point.x - 24, point.y - 24 , 48, 48) animated:YES];
} else {
[self setZoomScale:1.0f animated:YES];
}
}
@end