-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIScrollView+YSPanel.m
248 lines (200 loc) · 6.9 KB
/
UIScrollView+YSPanel.m
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
244
245
246
247
248
//
// UIScrollView+YSPanel.m
// SampleProject
//
// Created by Federico Erostarbe on 04/09/14.
// Copyright (c) 2014 Federico Erostarbe. All rights reserved.
//
#import "UIScrollView+YSPanel.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
typedef NS_ENUM(NSInteger, YSPanelObservingStatus) {
YSPanelObservingStatusNone,
YSPanelObservingStatusDefault,
YSPanelObservingStatusTextView,
YSPanelObservingStatusTableView
};
static char UIScrollViewYSPanelView;
#define kDefaultPanelFrame CGRectMake(0.0f, 0.0f, 50.0f, 30.9f)
#define kRightPadding 5.0f
@interface YSPanelView ()
@property (nonatomic, copy) DefaultBlock defaultBlock;
@property (nonatomic, copy) TextViewBlock textViewBlock;
@property (nonatomic, copy) TableViewBlock tableViewBlock;
@property (nonatomic, assign) YSPanelObservingStatus ObservingStatus;
@property (nonatomic, weak) UIScrollView *scrollView;
@end
@implementation UIScrollView (YSPanel)
@dynamic panelView;
- (void)setPanelView:(YSPanelView *)panelView
{
[self willChangeValueForKey:@"YSPanelView"];
objc_setAssociatedObject(self, &UIScrollViewYSPanelView,
panelView,
OBJC_ASSOCIATION_RETAIN);
[self didChangeValueForKey:@"YSPanelView"];
}
- (YSPanelView *)panelView {
return objc_getAssociatedObject(self, &UIScrollViewYSPanelView);
}
- (void) addDefaultPanelWithBlock:(DefaultBlock)block
{
[self addPanelWithBlock:block status:YSPanelObservingStatusDefault];
}
- (void) addTextViewPanelWithBlock:(TextViewBlock)block
{
[self addPanelWithBlock:block status:YSPanelObservingStatusTextView];
}
- (void) addTableViewPanelWithBlock:(TableViewBlock)block
{
[self addPanelWithBlock:block status:YSPanelObservingStatusTableView];
}
- (void) addPanelWithBlock:(id)block status:(YSPanelObservingStatus)status
{
if (!self.panelView) {
YSPanelView *view = [[YSPanelView alloc] initWithFrame:kDefaultPanelFrame];
view.scrollView = self;
view.ObservingStatus = status;
if (status == YSPanelObservingStatusDefault) {
view.defaultBlock = block;
} else if (status == YSPanelObservingStatusTextView) {
view.textViewBlock = block;
} else if (status == YSPanelObservingStatusTableView) {
view.tableViewBlock = block;
}
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75f];
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:10.0f];
label.numberOfLines = 0;
[view addSubview:label];
view.titleLabel = label;
self.panelView = view;
[self addObserver:view
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew
context:nil];
}
}
- (UIView *) indicator
{
return self.subviews.lastObject;
}
@end
@implementation YSPanelView
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (![keyPath isEqualToString:@"contentOffset"]) {
return;
}
[self updateFrame];
[self answerBlock:[self indicatorCenter]];
}
- (void) updateFrame
{
UIView *indicator = _scrollView.indicator;
if (!self.superview) {
[indicator addSubview:self];
}
CGRect infoPanelFrame = self.frame;
infoPanelFrame.origin.x = self.x;
infoPanelFrame.origin.y = [self y:indicator.frame];
self.frame = infoPanelFrame;
}
#pragma mark - UI
- (CGPoint) indicatorCenter
{
CGPoint p = CGPointZero;
if ([_scrollView isKindOfClass:[UIScrollView class]]) {
CGRect indicatorFrame = _scrollView.indicator.frame;
p = CGPointMake(0, indicatorFrame.origin.y + indicatorFrame.size.height / 2);
}
return p;
}
- (CGFloat) x
{
CGFloat x;
if (_centered) {
x = (_scrollView.frame.size.width / 2 + self.frame.size.width / 2 ) * (-1);
} else {
x = self.frame.size.width * (-1) - kRightPadding;
}
return x;
}
- (CGFloat) y:(CGRect)indicatorFrame
{
CGRect infoPanelFrame = self.frame;
return indicatorFrame.size.height / 2 - infoPanelFrame.size.height / 2;
}
#pragma -
- (void) setCustomView:(UIView *)view
{
_customView = view;
[self setNeedsLayout];
}
- (void) layoutSubviews
{
if (_customView != nil && _customView.superview == nil) {
self.layer.cornerRadius = 0.0f;
self.backgroundColor = [UIColor clearColor];
self.frame = _customView.frame;
[self addSubview:_customView];
}
}
- (void) willMoveToSuperview:(UIView *)newSuperview
{
if (self.superview && newSuperview == nil && _ObservingStatus != YSPanelObservingStatusNone) {
_ObservingStatus = YSPanelObservingStatusNone;
[self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
}
#pragma mark - Blocks
- (void) answerBlock:(CGPoint)p
{
if (_ObservingStatus == YSPanelObservingStatusDefault) {
[self answerDefaultBlock:p];
} else if (_ObservingStatus == YSPanelObservingStatusTextView) {
[self answerTextViewBlock:p];
} else if (_ObservingStatus == YSPanelObservingStatusTableView) {
[self answerTableViewBlock:p];
}
}
- (void) answerDefaultBlock:(CGPoint)p
{
if (_defaultBlock != nil) {
CGFloat contentOffsetY = _scrollView.contentOffset.y;
CGFloat contentHeight = _scrollView.contentSize.height;
CGFloat frameHeight = _scrollView.frame.size.height;
CGFloat percent = (contentOffsetY / (contentHeight - frameHeight)) * 100;
if (percent < 0) percent = 0; else if (percent > 100) percent = 100;
_defaultBlock(p, percent);
}
}
- (void) answerTextViewBlock:(CGPoint)p
{
if (_textViewBlock != nil && [_scrollView isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *) _scrollView;
NSInteger lines = textView.contentSize.height / textView.font.lineHeight;
CGFloat line = roundf(p.y / textView.font.lineHeight);
if (line < 1) line = 1; else if (line > lines) line = lines;
_textViewBlock(p, (NSInteger) line);
}
}
- (void) answerTableViewBlock:(CGPoint)p
{
if (_tableViewBlock != nil && [_scrollView isKindOfClass:[UITableView class]]) {
UITableView *tableView = (UITableView *) _scrollView;
CGFloat height = tableView.contentSize.height;
if (p.y < 0.0f) p.y = 0.0f; else if (p.y > height) p.y = height - 1.0f;
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:p];
_tableViewBlock(p, indexPath);
}
}
@end