Skip to content

Commit 66aff07

Browse files
committed
浏览图片的放大效果
1 parent 27e8cf6 commit 66aff07

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

Coding_iOS/Vendor/MJPhotoBrowser/MJPhotoView.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,14 @@ - (void)adjustFrame
166166

167167
// 基本尺寸参数
168168
CGFloat boundsWidth = self.bounds.size.width;
169+
CGFloat boundsHeight = self.bounds.size.height;
169170
CGFloat imageWidth = _imageView.image.size.width;
171+
CGFloat imageHeight = _imageView.image.size.height;
170172

171173
// 设置伸缩比例
172-
CGFloat minScale = boundsWidth / imageWidth;
173-
if (minScale > 1) {
174-
minScale = 1.0;
175-
}
174+
CGFloat imageScale = boundsWidth / imageWidth;
175+
CGFloat minScale = MIN(1.0, imageScale);
176+
176177
CGFloat maxScale = 2.0;
177178
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
178179
maxScale = maxScale / [[UIScreen mainScreen] scale];
@@ -181,7 +182,8 @@ - (void)adjustFrame
181182
self.minimumZoomScale = minScale;
182183
self.zoomScale = minScale;
183184

184-
CGRect imageFrame = self.bounds;
185+
CGRect imageFrame = CGRectMake(0, MAX(0, (boundsHeight- imageHeight*imageScale)/2), boundsWidth, imageHeight *imageScale);
186+
185187
if (_photo.firstShow) { // 第一次显示的图片
186188
_photo.firstShow = NO; // 已经显示过了
187189
_imageView.frame = imageFrame;
@@ -201,6 +203,8 @@ - (void)adjustFrame
201203

202204
#pragma mark - UIScrollViewDelegate
203205
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
206+
CGFloat insetY = (CGRectGetHeight(self.bounds) - CGRectGetHeight(_imageView.frame))/2;
207+
[_imageView setY:MAX(insetY, 0.0)];
204208
return _imageView;
205209
}
206210

@@ -235,12 +239,13 @@ - (void)hide
235239

236240
- (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
237241
_doubleTap = YES;
238-
239-
CGPoint touchPoint = [tap locationInView:self];
240242
if (self.zoomScale == self.maximumZoomScale) {
241243
[self setZoomScale:self.minimumZoomScale animated:YES];
242244
} else {
243-
[self zoomToRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1) animated:YES];
245+
CGPoint touchPoint = [tap locationInView:self];
246+
CGFloat scale = self.maximumZoomScale/ self.minimumZoomScale;
247+
CGRect rectTozoom=CGRectMake(touchPoint.x * scale, touchPoint.y * scale, 1, 1);
248+
[self zoomToRect:rectTozoom animated:YES];
244249
}
245250
}
246251

0 commit comments

Comments
 (0)