forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulPostsView.m
More file actions
179 lines (142 loc) · 5.76 KB
/
AwfulPostsView.m
File metadata and controls
179 lines (142 loc) · 5.76 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
// AwfulPostsView.m
//
// Copyright 2014 Awful Contributors. CC BY-NC-SA 3.0 US https://github.com/Awful/Awful.app
#import "AwfulPostsView.h"
#import "AwfulFrameworkCategories.h"
typedef enum : NSInteger {
TopBarHidden,
TopBarPartiallyVisible,
TopBarVisible,
} TopBarState;
@interface AwfulPostsView () <UIScrollViewDelegate>
@property (strong, nonatomic) UIWebView *webView;
@property (assign, nonatomic) CGFloat exposedTopBarSlice;
@property (assign, nonatomic) CGPoint lastContentOffset;
@property (readonly, assign, nonatomic) TopBarState topBarState;
@property (assign, nonatomic) BOOL ignoreScrollViewDidScroll;
@property (assign, nonatomic) BOOL maintainTopBarState;
@property (assign, nonatomic) BOOL topBarAlwaysVisible;
@end
@implementation AwfulPostsView
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_webView = [UIWebView awful_nativeFeelingWebView];
_webView.backgroundColor = nil;
[self addSubview:_webView];
_webView.scrollView.delegate = self;
_topBar = [AwfulPostsViewTopBar new];
[self addSubview:_topBar];
self.maintainTopBarState = YES;
[self updateForVoiceOverAnimated:NO];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(voiceOverStatusDidChange:) name:UIAccessibilityVoiceOverStatusChanged object:nil];
}
return self;
}
- (void)layoutSubviews
{
CGRect topBarFrame = self.topBar.bounds;
topBarFrame.origin.y = self.exposedTopBarSlice - CGRectGetHeight(topBarFrame);
topBarFrame.size.width = CGRectGetWidth(self.bounds);
self.topBar.frame = topBarFrame;
self.webView.frame = CGRectMake(0, CGRectGetMaxY(topBarFrame), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - self.exposedTopBarSlice);
}
- (void)updateForVoiceOverAnimated:(BOOL)animated
{
self.topBarAlwaysVisible = UIAccessibilityIsVoiceOverRunning();
if (self.topBarAlwaysVisible) {
self.exposedTopBarSlice = CGRectGetHeight(self.topBar.bounds);
[UIView animateWithDuration:(animated ? 0.2 : 0) animations:^{
[self layoutIfNeeded];
}];
}
}
- (void)voiceOverStatusDidChange:(NSNotification *)notification
{
[self updateForVoiceOverAnimated:YES];
}
- (void)furtherExposeTopBarSlice:(CGFloat)delta
{
CGFloat oldExposedSlice = self.exposedTopBarSlice;
#define CLAMP(low, x, high) MIN(MAX(low, x), high)
self.exposedTopBarSlice = CLAMP(0, self.exposedTopBarSlice + delta, CGRectGetHeight(self.topBar.bounds));
CGFloat exposedSliceDelta = self.exposedTopBarSlice - oldExposedSlice;
self.ignoreScrollViewDidScroll = YES;
CGPoint contentOffset = self.webView.scrollView.contentOffset;
contentOffset.y = MAX(contentOffset.y + exposedSliceDelta, 0);
self.webView.scrollView.contentOffset = contentOffset;
self.ignoreScrollViewDidScroll = NO;
}
- (TopBarState)topBarState
{
if (self.exposedTopBarSlice == 0) {
return TopBarHidden;
} else if (self.exposedTopBarSlice >= CGRectGetHeight(self.topBar.bounds)) {
return TopBarVisible;
} else {
return TopBarPartiallyVisible;
}
}
- (void)setExposedTopBarSlice:(CGFloat)exposedTopBarSlice
{
if (exposedTopBarSlice != _exposedTopBarSlice) {
_exposedTopBarSlice = exposedTopBarSlice;
[self setNeedsLayout];
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastContentOffset = scrollView.contentOffset;
self.maintainTopBarState = NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.ignoreScrollViewDidScroll || self.topBarAlwaysVisible) return;
CGFloat scrollDistance = scrollView.contentOffset.y - self.lastContentOffset.y;
if (scrollDistance == 0) return;
switch (self.topBarState) {
case TopBarHidden: {
// Don't start showing a hidden topbar after bouncing.
if (self.maintainTopBarState) break;
// Only moving the content down can expose the topbar.
if (scrollDistance < 0) {
// Only start showing the topbar if we're scrolling past the bottom of the scrollview's contents. Otherwise we can briefly trap ourselves at the bottom, exposing some topbar causing the scrollview to bounce back.
if (CGRectGetMaxY(scrollView.bounds) - scrollView.contentInset.bottom - scrollDistance <= scrollView.contentSize.height) {
[self furtherExposeTopBarSlice:-scrollDistance];
}
}
break;
}
case TopBarPartiallyVisible: {
[self furtherExposeTopBarSlice:-scrollDistance];
break;
}
case TopBarVisible: {
// Don't start hiding a visible topbar after bouncing.
if (self.maintainTopBarState) break;
// Only start hiding the topbar if we're scrolling past the top of the scrollview's contents. Otherwise we can briefly trap ourselves at the top, hiding some topbar causing the scrollview to bounce back.
if (scrollView.contentOffset.y < 0) break;
// Only moving the content up can hide the topbar.
if (scrollDistance > 0) {
[self furtherExposeTopBarSlice:-scrollDistance];
}
break;
}
}
self.lastContentOffset = scrollView.contentOffset;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
self.maintainTopBarState = YES;
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
self.maintainTopBarState = YES;
return YES;
}
@end