forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulScrollViewTopBar.m
More file actions
111 lines (90 loc) · 3.54 KB
/
Copy pathAwfulScrollViewTopBar.m
File metadata and controls
111 lines (90 loc) · 3.54 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
//
// AwfulScrollViewTopBar.m
// Awful
//
// Created by Nolan Waite on 2013-02-13.
// Copyright (c) 2013 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulScrollViewTopBar.h"
@interface AwfulScrollViewTopBar ()
@property (weak, nonatomic) UIScrollView *scrollView;
@property (nonatomic) CGPoint lastContentOffset;
@property (nonatomic) BOOL scrollingUp;
@end
@implementation AwfulScrollViewTopBar
#pragma mark - UIView
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame])) return nil;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
if ([newSuperview isKindOfClass:[UIScrollView class]]) {
self.scrollView = (id)newSuperview;
CGRect frame = self.frame;
frame.origin.x = 0;
frame.origin.y = -CGRectGetHeight(self.frame);
self.frame = frame;
[self setNeedsLayout];
IncreaseScrollViewTopOffset(self.scrollView, CGRectGetHeight(self.frame));
} else {
IncreaseScrollViewTopOffset(self.scrollView, -CGRectGetHeight(self.frame));
self.scrollView = nil;
}
}
static void IncreaseScrollViewTopOffset(UIScrollView *scrollView, CGFloat offset)
{
UIEdgeInsets contentInset = scrollView.contentInset;
contentInset.top += offset;
scrollView.contentInset = contentInset;
UIEdgeInsets scrollIndicatorInsets = scrollView.scrollIndicatorInsets;
scrollIndicatorInsets.top += offset;
scrollView.scrollIndicatorInsets = scrollIndicatorInsets;
}
#pragma mark - UIScrollViewDelegate (forwarded)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastContentOffset = scrollView.contentOffset;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.frame;
if (scrollView.contentOffset.y <= -CGRectGetHeight(frame)) {
// Don't slide or bounce under the navigation bar.
frame.origin.y = scrollView.contentOffset.y;
} else {
if (!self.scrollingUp) {
// When scrolling down, scroll along with the content...
if (!CGRectIntersectsRect(frame, scrollView.bounds)) {
// ...until we're out of sight, then perch atop the scroll view.
frame.origin.y = -CGRectGetHeight(frame);
}
} else if (CGRectGetMinY(frame) > scrollView.contentOffset.y) {
// When scrolling up, if we were visible, stay visible.
frame.origin.y = CGRectGetMinY(scrollView.bounds);
}
}
self.frame = frame;
if (CGPointEqualToPoint(self.lastContentOffset, scrollView.contentOffset)) return;
self.scrollingUp = scrollView.contentOffset.y < self.lastContentOffset.y;
self.lastContentOffset = scrollView.contentOffset;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// Now may be the time to let go of our perch atop the scroll view.
// But only if we're decelerating upwards.
if (!decelerate || !self.scrollingUp) return;
CGRect frame = self.frame;
// And only if we're not stuck under the navigation bar.
if (CGRectGetMinY(frame) < -CGRectGetHeight(frame)) return;
// And only if we're not already visible.
if (CGRectGetMinY(frame) >= CGRectGetMinY(scrollView.bounds) - CGRectGetHeight(frame)) return;
frame.origin.y = CGRectGetMinY(scrollView.bounds) - CGRectGetHeight(frame);
if (CGRectGetMinY(frame) < -CGRectGetHeight(frame)) {
frame.origin.y = -CGRectGetHeight(frame);
}
self.frame = frame;
}
@end