-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathDirectedScrollViewManager.m
More file actions
206 lines (165 loc) · 7.84 KB
/
DirectedScrollViewManager.m
File metadata and controls
206 lines (165 loc) · 7.84 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
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
//
// DirectedScrollViewManager.m
// DirectedScrollViewManager
//
#import "DirectedScrollViewManager.h"
#import "DirectedScrollViewChildManager.h"
#import <React/RCTScrollView.h>
#import <React/RCTUIManager.h>
#import <React/RCTEventDispatcher.h>
@interface DirectedScrollView : RCTScrollView
@property (nonatomic, weak) id <DirectedScrollViewDelegate> delegate;
@property (nonatomic, assign) BOOL verticalBounceEnabled;
@property (nonatomic, assign) BOOL horizontalBounceEnabled;
@end
@implementation DirectedScrollView
#pragma mark - ScrollView delegate
@synthesize verticalBounceEnabled = _verticalBounceEnabled;
@synthesize horizontalBounceEnabled = _horizontalBounceEnabled;
// Tried to overwrite getter to manager default value
- (BOOL) verticalBounceEnabled {
return _verticalBounceEnabled ? _verticalBounceEnabled : TRUE;
}
- (BOOL) horizontalBounceEnabled {
return _horizontalBounceEnabled ? _horizontalBounceEnabled : TRUE;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[super scrollViewDidScroll:scrollView];
UIView *contentView = [self contentView];
RCTLogInfo(@"verticalBounceEnabled -> %d", _verticalBounceEnabled);
RCTLogInfo(@"horizontalBounceEnabled -> %d", _horizontalBounceEnabled);
if(!_verticalBounceEnabled) {
if (scrollView.contentInset.bottom <= 0) {
[scrollView setContentInset:UIEdgeInsetsMake(scrollView.contentInset.top,scrollView.contentInset.right,0,scrollView.contentInset.left)];
} else if (scrollView.contentOffset.y < 0) {
[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0) animated: false];
}
}
if(!_horizontalBounceEnabled) {
if (scrollView.contentOffset.x >= scrollView.contentSize.width - scrollView.frame.size.width) {
[scrollView setContentOffset:CGPointMake(scrollView.contentSize.width - scrollView.frame.size.width, scrollView.contentOffset.y) animated: false];
} else if (scrollView.contentOffset.y < 0) {
[scrollView setContentOffset:CGPointMake(0, scrollView.contentOffset.y) animated: false];
}
}
for (UIView *subview in contentView.reactSubviews)
{
DirectedScrollViewChild *scrollableChild = (DirectedScrollViewChild*)subview;
if (subview == nil) continue;
if (![scrollableChild shouldScrollVertically]) {
CGFloat scrollTop = scrollView.contentOffset.y + self.contentInset.top;
// adjust the y offset based on the current zoom scale
// if we're zoomed in the offset required will be less, if we're zoomed out it will be more
CGFloat yOffset = scrollTop / scrollView.zoomScale;
// translate the horizontally scrolling subview by the calculated y offset
// this cancels out the vertical translation applied by the scrollview and keeps the y position fixed
scrollableChild.transform = CGAffineTransformMakeTranslation(0, yOffset);
}
if (![scrollableChild shouldScrollHorizontally]) {
CGFloat scrollLeft = scrollView.contentOffset.x + self.contentInset.left;
// adjust the x offset based on the current zoom scale
// if we're zoomed in the offset required will be less, if we're zoomed out it will be more
CGFloat xOffset = scrollLeft / scrollView.zoomScale;
// translate the vertically scrolling subview by the calculated x offset
// this cancels out the horizontal translation applied by the scrollview and keeps the x position fixed
scrollableChild.transform = CGAffineTransformMakeTranslation(xOffset, 0);
}
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[super scrollViewWillBeginDragging:scrollView];
if ([self.delegate respondsToSelector:@selector(scrollViewWillBeginDragging)]) {
[self.delegate scrollViewWillBeginDragging];
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
if ([self.delegate respondsToSelector:@selector(scrollViewDidEndDragging)]) {
[self.delegate scrollViewDidEndDragging];
}
}
@end
@implementation DirectedScrollViewManager
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (UIView *)view
{
DirectedScrollView *directedScrollView = [[DirectedScrollView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
directedScrollView.delegate = self;
return directedScrollView;
}
// RCTDirectedScrollViewDelegate methods
-(void)scrollViewWillBeginDragging {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"scrollViewWillBeginDragging" body:nil];
}
-(void)scrollViewDidEndDragging {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"scrollViewDidEndDragging" body:nil];
}
// RCTScrollView properties
RCT_EXPORT_VIEW_PROPERTY(bounces, BOOL)
RCT_EXPORT_VIEW_PROPERTY(alwaysBounceHorizontal, BOOL)
RCT_EXPORT_VIEW_PROPERTY(alwaysBounceVertical, BOOL)
RCT_EXPORT_VIEW_PROPERTY(bouncesZoom, BOOL)
RCT_EXPORT_VIEW_PROPERTY(maximumZoomScale, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(minimumZoomScale, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(canCancelContentTouches, BOOL)
RCT_EXPORT_VIEW_PROPERTY(centerContent, BOOL)
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
RCT_EXPORT_VIEW_PROPERTY(decelerationRate, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(directionalLockEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(pinchGestureEnabled, scrollView.pinchGestureEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(scrollIndicatorInsets, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(snapToInterval, int)
RCT_EXPORT_VIEW_PROPERTY(scrollEventThrottle, NSTimeInterval)
RCT_EXPORT_VIEW_PROPERTY(snapToAlignment, NSString)
RCT_EXPORT_VIEW_PROPERTY(onScrollBeginDrag, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onScrollEndDrag, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMomentumScrollBegin, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMomentumScrollEnd, RCTDirectEventBlock)
// RCTScrollView methods
RCT_EXPORT_METHOD(scrollTo:(nonnull NSNumber *)reactTag
offsetX:(CGFloat)x
offsetY:(CGFloat)y
animated:(BOOL)animated)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
UIView *view = viewRegistry[reactTag];
if ([view conformsToProtocol:@protocol(RCTScrollableProtocol)]) {
[(id<RCTScrollableProtocol>)view scrollToOffset:(CGPoint){x, y} animated:animated];
} else {
RCTLogError(@"tried to scrollTo: on non-RCTScrollableProtocol view %@ with tag #%@", view, reactTag);
}
}];
}
RCT_EXPORT_METHOD(zoomToStart:(nonnull NSNumber *)reactTag
animated:(BOOL)animated)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
UIView *view = viewRegistry[reactTag];
if ([view conformsToProtocol:@protocol(RCTScrollableProtocol)]) {
[(id<RCTScrollableProtocol>)view zoomToRect:CGRectMake(0, 0, 0, 0) animated:animated];
[((RCTScrollView*)view).scrollView setZoomScale:1.0 animated:animated];
} else {
RCTLogError(@"tried to zoomToRect: on non-RCTScrollableProtocol view %@ with tag #%@", view, reactTag);
}
}];
}
RCT_CUSTOM_VIEW_PROPERTY(verticalBounceEnabled, BOOL, DirectedScrollView)
{
// Failed to set property
view.verticalBounceEnabled = json;
}
RCT_CUSTOM_VIEW_PROPERTY(horizontalBounceEnabled, BOOL, DirectedScrollView)
{
// Failed to set property
view.horizontalBounceEnabled = json;
}
@end