forked from pro648/BasicDemos-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
142 lines (111 loc) · 5 KB
/
Copy pathViewController.m
File metadata and controls
142 lines (111 loc) · 5 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
//
// ViewController.m
// ScrollView
//
// Created by ad on 03/07/2017.
//
// 详细博文:https://github.com/pro648/tips/wiki/UIScrollView%E7%9A%84%E7%94%A8%E6%B3%95
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.imageView];
// 设定最小缩放比
[self setZoomScale];
// 添加双击手势
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[self.scrollView addGestureRecognizer:doubleTap];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// 1.移除子视图
for (UIView *view in self.scrollView.subviews) {
[view removeFromSuperview];
}
// 2.初始化imageView 将其添加到scrollView 设置contentSize
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = self.imageView.frame.size;
// 3.重设minimumZoomScale
[self setZoomScale];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Properties
- (UIImageView *)imageView {
if (!_imageView) {
UIImage *image = [UIImage imageNamed:@"image"];
// 1.初始化imageView
_imageView = [[UIImageView alloc] initWithImage:image];
}
return _imageView;
}
- (UIScrollView *)scrollView {
if (!_scrollView) {
// 2.初始化、配置scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.contentSize = self.imageView.frame.size;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.contentOffset = CGPointMake(1000, 450);
_scrollView.delegate = self;
// _scrollView.minimumZoomScale = 0.1;
// _scrollView.maximumZoomScale = 4.0;
// _scrollView.zoomScale = 1.0;
}
return _scrollView;
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// 计算imageView缩小到最小时 imageView与屏幕边缘距离
CGFloat horizontalPadding = CGRectGetWidth(self.imageView.frame) < CGRectGetWidth(scrollView.frame) ? (CGRectGetWidth(scrollView.frame) - CGRectGetWidth(self.imageView.frame)) / 2 : 0 ;
CGFloat verticalPadding = CGRectGetHeight(self.imageView.frame) < CGRectGetHeight(scrollView.frame) ? (CGRectGetHeight(scrollView.frame) - CGRectGetHeight(self.imageView.frame)) / 2 : 0 ;
scrollView.contentInset = UIEdgeInsetsMake(verticalPadding, horizontalPadding, verticalPadding, horizontalPadding);
}
#pragma mark - Help Method
- (void)setZoomScale {
CGFloat widthScale = CGRectGetWidth(self.scrollView.frame) / CGRectGetWidth(self.imageView.frame);
CGFloat heightScale = CGRectGetHeight(self.scrollView.frame) / CGRectGetHeight(self.imageView.frame);
self.scrollView.minimumZoomScale = MIN(widthScale, heightScale);
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)doubleTap {
if (self.scrollView.zoomScale > self.scrollView.minimumZoomScale) {
// 视图大于最小视图时 双击将视图缩小至最小
[self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
}
else
{
/*
// 视图为最小时 双击将视图放大至最大
[self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES];
*/
// 1.获取点击位置
CGPoint touchPoint = [doubleTap locationInView:self.imageView];
// 2.获取要显示的imageView区域
CGRect zoomRect = [self zoomRectForScrollView:self.scrollView withScale:self.scrollView.maximumZoomScale withCenter:touchPoint];
// 5.将要显示的imageView区域显示到scrollView
[self.scrollView zoomToRect:zoomRect animated:YES];
}
}
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(CGFloat)scale withCenter:(CGPoint)center {
// 3.声明一个区域 滚动视图的宽除以放大倍数可以得到要显示imageView宽度
CGRect zoomRect;
zoomRect.size.width = CGRectGetWidth(scrollView.frame) / scale;
zoomRect.size.height = CGRectGetHeight(scrollView.frame) / scale;
// 4.点击位置x坐标减去1/2图像宽度,可以得到要显示imageView的原点x坐标 y坐标类似
zoomRect.origin.x = center.x - zoomRect.size.width / 2;
zoomRect.origin.y = center.y - zoomRect.size.height / 2;
return zoomRect;
}
@end