2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTScrollViewManager.h"
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTScrollView.h"
|
2016-09-26 13:11:49 +00:00
|
|
|
#import "RCTShadowView.h"
|
2015-03-11 02:03:59 +00:00
|
|
|
#import "RCTUIManager.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-07-07 14:38:24 +00:00
|
|
|
@interface RCTScrollView (Private)
|
2015-08-24 10:14:33 +00:00
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSArray<NSDictionary *> *)calculateChildFramesData;
|
2015-08-24 10:14:33 +00:00
|
|
|
|
2015-07-07 14:38:24 +00:00
|
|
|
@end
|
|
|
|
|
2015-06-05 15:46:17 +00:00
|
|
|
@implementation RCTConvert (UIScrollView)
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(UIScrollViewKeyboardDismissMode, (@{
|
|
|
|
@"none": @(UIScrollViewKeyboardDismissModeNone),
|
|
|
|
@"on-drag": @(UIScrollViewKeyboardDismissModeOnDrag),
|
|
|
|
@"interactive": @(UIScrollViewKeyboardDismissModeInteractive),
|
|
|
|
// Backwards compatibility
|
|
|
|
@"onDrag": @(UIScrollViewKeyboardDismissModeOnDrag),
|
|
|
|
}), UIScrollViewKeyboardDismissModeNone, integerValue)
|
|
|
|
|
2016-01-27 18:16:10 +00:00
|
|
|
RCT_ENUM_CONVERTER(UIScrollViewIndicatorStyle, (@{
|
|
|
|
@"default": @(UIScrollViewIndicatorStyleDefault),
|
|
|
|
@"black": @(UIScrollViewIndicatorStyleBlack),
|
|
|
|
@"white": @(UIScrollViewIndicatorStyleWhite),
|
|
|
|
}), UIScrollViewIndicatorStyleDefault, integerValue)
|
|
|
|
|
2015-06-05 15:46:17 +00:00
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTScrollViewManager
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (UIView *)view
|
|
|
|
{
|
2015-03-06 00:36:41 +00:00
|
|
|
return [[RCTScrollView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(alwaysBounceHorizontal, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(alwaysBounceVertical, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(bounces, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(bouncesZoom, 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)
|
2016-01-27 18:16:10 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(indicatorStyle, UIScrollViewIndicatorStyle)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(keyboardDismissMode, UIScrollViewKeyboardDismissMode)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(maximumZoomScale, CGFloat)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(minimumZoomScale, CGFloat)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
|
2016-10-05 14:15:25 +00:00
|
|
|
#if !TARGET_OS_TV
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(scrollsToTop, BOOL)
|
2016-10-05 14:15:25 +00:00
|
|
|
#endif
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL)
|
2015-05-06 17:46:45 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(stickyHeaderIndices, NSIndexSet)
|
2015-03-30 11:58:02 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(scrollEventThrottle, NSTimeInterval)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(zoomScale, CGFloat)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(scrollIndicatorInsets, UIEdgeInsets)
|
2015-09-23 18:43:57 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(snapToInterval, int)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(snapToAlignment, NSString)
|
2015-03-30 11:58:02 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(contentOffset, scrollView.contentOffset, CGPoint)
|
2016-04-28 14:43:24 +00:00
|
|
|
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)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onScrollAnimationEnd, RCTDirectEventBlock)
|
2015-03-30 11:58:02 +00:00
|
|
|
|
2016-09-26 13:11:49 +00:00
|
|
|
// overflow is used both in css-layout as well as by reac-native. In css-layout
|
|
|
|
// we always want to treat overflow as scroll but depending on what the overflow
|
|
|
|
// is set to from js we want to clip drawing or not. This piece of code ensures
|
|
|
|
// that css-layout is always treating the contents of a scroll container as
|
|
|
|
// overflow: 'scroll'.
|
|
|
|
RCT_CUSTOM_SHADOW_PROPERTY(overflow, CSSOverflow, RCTShadowView) {
|
|
|
|
view.overflow = CSSOverflowScroll;
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(getContentSize:(nonnull NSNumber *)reactTag
|
2015-04-08 15:52:48 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-03-11 02:03:59 +00:00
|
|
|
{
|
2016-01-14 15:41:24 +00:00
|
|
|
[self.bridge.uiManager addUIBlock:
|
|
|
|
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTScrollView *> *viewRegistry) {
|
2015-03-11 02:03:59 +00:00
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
RCTScrollView *view = viewRegistry[reactTag];
|
|
|
|
if (!view || ![view isKindOfClass:[RCTScrollView class]]) {
|
|
|
|
RCTLogError(@"Cannot find RCTScrollView with tag #%@", reactTag);
|
2015-03-11 02:03:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
CGSize size = view.scrollView.contentSize;
|
2015-03-11 02:03:59 +00:00
|
|
|
callback(@[@{
|
|
|
|
@"width" : @(size.width),
|
|
|
|
@"height" : @(size.height)
|
|
|
|
}]);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(calculateChildFrames:(nonnull NSNumber *)reactTag
|
2016-01-14 15:41:24 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-07-07 14:38:24 +00:00
|
|
|
{
|
2016-01-14 15:41:24 +00:00
|
|
|
[self.bridge.uiManager addUIBlock:
|
|
|
|
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTScrollView *> *viewRegistry) {
|
2015-07-07 14:38:24 +00:00
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
RCTScrollView *view = viewRegistry[reactTag];
|
|
|
|
if (!view || ![view isKindOfClass:[RCTScrollView class]]) {
|
|
|
|
RCTLogError(@"Cannot find RCTScrollView with tag #%@", reactTag);
|
2015-07-07 14:38:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<NSDictionary *> *childFrames = [view calculateChildFramesData];
|
2015-07-07 14:38:24 +00:00
|
|
|
if (childFrames) {
|
|
|
|
callback(@[childFrames]);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:41:24 +00:00
|
|
|
RCT_EXPORT_METHOD(scrollTo:(nonnull NSNumber *)reactTag
|
2016-02-01 19:04:43 +00:00
|
|
|
offsetX:(CGFloat)x
|
|
|
|
offsetY:(CGFloat)y
|
2016-01-14 15:41:24 +00:00
|
|
|
animated:(BOOL)animated)
|
2015-11-25 11:07:06 +00:00
|
|
|
{
|
2016-01-14 15:41:24 +00:00
|
|
|
[self.bridge.uiManager addUIBlock:
|
|
|
|
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
|
2015-11-25 11:07:06 +00:00
|
|
|
UIView *view = viewRegistry[reactTag];
|
|
|
|
if ([view conformsToProtocol:@protocol(RCTScrollableProtocol)]) {
|
2016-02-01 19:04:43 +00:00
|
|
|
[(id<RCTScrollableProtocol>)view scrollToOffset:(CGPoint){x, y} animated:animated];
|
2015-11-25 11:07:06 +00:00
|
|
|
} else {
|
2016-01-14 15:41:24 +00:00
|
|
|
RCTLogError(@"tried to scrollTo: on non-RCTScrollableProtocol view %@ "
|
|
|
|
"with tag #%@", view, reactTag);
|
2015-11-25 11:07:06 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:41:24 +00:00
|
|
|
RCT_EXPORT_METHOD(zoomToRect:(nonnull NSNumber *)reactTag
|
|
|
|
withRect:(CGRect)rect
|
|
|
|
animated:(BOOL)animated)
|
2015-11-25 11:07:06 +00:00
|
|
|
{
|
2016-01-14 15:41:24 +00:00
|
|
|
[self.bridge.uiManager addUIBlock:
|
|
|
|
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
|
2015-11-25 11:07:06 +00:00
|
|
|
UIView *view = viewRegistry[reactTag];
|
|
|
|
if ([view conformsToProtocol:@protocol(RCTScrollableProtocol)]) {
|
2016-01-13 10:54:19 +00:00
|
|
|
[(id<RCTScrollableProtocol>)view zoomToRect:rect animated:animated];
|
2015-11-25 11:07:06 +00:00
|
|
|
} else {
|
2016-01-14 15:41:24 +00:00
|
|
|
RCTLogError(@"tried to zoomToRect: on non-RCTScrollableProtocol view %@ "
|
|
|
|
"with tag #%@", view, reactTag);
|
2015-11-25 11:07:06 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2015-11-19 19:13:42 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|