-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoView.m
More file actions
219 lines (182 loc) · 8.9 KB
/
Copy pathPhotoView.m
File metadata and controls
219 lines (182 loc) · 8.9 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
//
// PhotoView.m
// aoyouHH
//
// Created by jinzelu on 15/4/30.
// Copyright (c) 2015年 jinzelu. All rights reserved.
//
#import "PhotoView.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "BHProgressHUD.h"
@interface PhotoView ()<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
MBProgressHUD *hud;
}
@end
@implementation PhotoView
-(id)initWithFrame:(CGRect)frame withPhotoUrl:(NSString *)photoUrl{
self = [super initWithFrame:frame];
if (self) {
//添加scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_scrollView];
//添加图片
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.userInteractionEnabled = YES;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
BOOL isCached = [manager cachedImageExistsForURL:[NSURL URLWithString:photoUrl]];
if (!isCached) {//没有缓存
hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
}
[self.imageView sd_setImageWithURL:[NSURL URLWithString:photoUrl] placeholderImage:[UIImage imageNamed:@"comment_empty_img"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize){
hud.progress = ((float)receivedSize)/expectedSize;
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
NSLog(@"图片加载完成");
if (!isCached) {
[hud hide:YES];
}
}];
[self.imageView setUserInteractionEnabled:YES];
[_scrollView addSubview:self.imageView];
//添加手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
//长按手势
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imglongTapClick)];
[self.imageView addGestureRecognizer:longTap];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;//需要点两下
twoFingerTap.numberOfTouchesRequired = 2;//需要两个手指touch
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[singleTap requireGestureRecognizerToFail:doubleTap];//如果双击了,则不响应单击事件
[_scrollView setZoomScale:1];
}
return self;
}
-(id)initWithFrame:(CGRect)frame withPhotoImage:(UIImage *)image{
self = [super initWithFrame:frame];
if (self) {
//添加scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_scrollView];
//添加图片
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.imageView setImage:image];
[self.imageView setUserInteractionEnabled:YES];
[_scrollView addSubview:self.imageView];
//添加手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
//长按手势
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imglongTapClick)];
[self.imageView addGestureRecognizer:longTap];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;//需要点两下
twoFingerTap.numberOfTouchesRequired = 2;//需要两个手指touch
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[singleTap requireGestureRecognizerToFail:doubleTap];//如果双击了,则不响应单击事件
[_scrollView setZoomScale:1];
}
return self;
}
#pragma mark - 长按处理
//长按动作
- (void)imglongTapClick {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"保存到相册" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
[(UIViewController *)self.delegate presentViewController: alertController animated: YES completion: nil];
}
//回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
[BHProgressHUD showToastTo:self text:msg];
}
#pragma mark - UIScrollViewDelegate
//scroll view处理缩放和平移手势,必须需要实现委托下面两个方法,另外 maximumZoomScale和minimumZoomScale两个属性要不一样
//1.返回要缩放的图片
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
//2.重新确定缩放完后的缩放倍数
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
}
#pragma mark - 图片的点击,touch事件
-(void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"单击");
if (gestureRecognizer.numberOfTapsRequired == 1) {
[self.delegate TapHiddenPhotoView];
}
}
-(void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"双击");
if (gestureRecognizer.numberOfTapsRequired == 2) {
if(_scrollView.zoomScale == 1){
float newScale = [_scrollView zoomScale] *2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}else{
float newScale = [_scrollView zoomScale]/2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}
}
}
-(void)handleTwoFingerTap:(UITapGestureRecognizer *)gestureRecongnizer{
NSLog(@"2手指操作");
float newScale = [_scrollView zoomScale]/2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecongnizer locationInView:gestureRecongnizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}
#pragma mark - 缩放大小获取方法
-(CGRect)zoomRectForScale:(CGFloat)scale withCenter:(CGPoint)center{
CGRect zoomRect;
//大小
zoomRect.size.height = [_scrollView frame].size.height/scale;
zoomRect.size.width = [_scrollView frame].size.width/scale;
//原点
zoomRect.origin.x = center.x - zoomRect.size.width/2;
zoomRect.origin.y = center.y - zoomRect.size.height/2;
return zoomRect;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end