Skip to content

Commit

Permalink
Move code around
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed May 22, 2017
1 parent 38b9263 commit f542b3f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
1 change: 0 additions & 1 deletion React/Views/RCTShadowView.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ - (void)applyLayoutToChildren:(YGNodeRef)node
[self didUpdateReactSubviews];
[applierBlocks addObject:^(NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[self->_reactTag];
[view clearSortedSubviews];
[view didUpdateReactSubviews];
}];
}
Expand Down
18 changes: 1 addition & 17 deletions React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
BOOL needsHitSubview = !(_pointerEvents == RCTPointerEventsNone || _pointerEvents == RCTPointerEventsBoxOnly);
if (needsHitSubview && (![self clipsToBounds] || isPointInside)) {
// Take z-index into account when calculating the touch target.
// Check if sorting is required - in most cases it won't be.
BOOL sortingRequired = NO;
for (UIView *subview in self.subviews) {
if (subview.layer.zPosition != 0) {
sortingRequired = YES;
break;
}
}
NSArray<UIView *> *sortedSubviews = sortingRequired ? [self.reactSubviews sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {
if (a.layer.zPosition > b.layer.zPosition) {
return NSOrderedDescending;
} else {
// Ensure sorting is stable by treating equal zIndex as ascending so
// that original order is preserved.
return NSOrderedAscending;
}
}] : self.subviews;
NSArray<UIView *> *sortedSubviews = [self reactZIndexSortedSubviews];

// The default behaviour of UIKit is that if a view does not contain a point,
// then no subviews will be returned from hit testing, even if they contain
Expand Down
10 changes: 1 addition & 9 deletions React/Views/RCTViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,7 @@ - (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(__unused NSDictio
RCT_VIEW_BORDER_RADIUS_PROPERTY(BottomLeft)
RCT_VIEW_BORDER_RADIUS_PROPERTY(BottomRight)

RCT_CUSTOM_VIEW_PROPERTY(zIndex, NSInteger, RCTView)
{
if (json) {
NSInteger index = [RCTConvert NSInteger:json];
view.layer.zPosition = index;
} else {
view.layer.zPosition = 0;
}
}
RCT_REMAP_VIEW_PROPERTY(zIndex, reactZIndex, NSInteger)

#pragma mark - ShadowView properties

Expand Down
3 changes: 0 additions & 3 deletions React/Views/UIView+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@
- (void)react_updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(UIView *)clipView;
- (UIView *)react_findClipView;

// zIndex sorting
- (void)clearSortedSubviews;

@end
4 changes: 4 additions & 0 deletions React/Views/UIView+React.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
*/
@property (nonatomic, assign) UIUserInterfaceLayoutDirection reactLayoutDirection;

@property (nonatomic, assign) NSInteger reactZIndex;

- (NSArray<UIView *> *)reactZIndexSortedSubviews;

/**
* Updates the subviews array based on the reactSubviews. Default behavior is
* to insert the sortedReactSubviews into the UIView.
Expand Down
31 changes: 31 additions & 0 deletions React/Views/UIView+React.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ - (void)setReactLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
}
}

- (NSInteger)reactZIndex
{
return self.layer.zPosition;
}

- (void)setReactZIndex:(NSInteger)reactZIndex
{
self.layer.zPosition = reactZIndex;
}

- (NSArray<UIView *> *)reactZIndexSortedSubviews
{
// Check if sorting is required - in most cases it won't be.
BOOL sortingRequired = NO;
for (UIView *subview in self.subviews) {
if (subview.reactZIndex != 0) {
sortingRequired = YES;
break;
}
}
return sortingRequired ? [self.reactSubviews sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {
if (a.reactZIndex > b.reactZIndex) {
return NSOrderedDescending;
} else {
// Ensure sorting is stable by treating equal zIndex as ascending so
// that original order is preserved.
return NSOrderedAscending;
}
}] : self.subviews;
}

- (void)didUpdateReactSubviews
{
for (UIView *subview in self.reactSubviews) {
Expand Down

0 comments on commit f542b3f

Please sign in to comment.