Skip to content

Commit

Permalink
Fabric: Removing taking zIndex into account in hit-testing algorithm
Browse files Browse the repository at this point in the history
Summary:
Now we have `zIndex` feature implemented in the Core, we don't need to take `view.layer.zIndex` into account when we do hit-testing, so now we don't need to sort *all subviews* on *all levels of hierarchy* every time we process touch down event.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D20425987

fbshipit-source-id: 025bd968ae948b9b0a4188845efc0de950fb5cdf
  • Loading branch information
shergin authored and facebook-github-bot committed Mar 16, 2020
1 parent efa74c4 commit 65d6f5a
Showing 1 changed file with 3 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ - (void)prepareForRecycle
- (UIView *)betterHitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// This is a classic textbook implementation of `hitTest:` with a couple of improvements:
// * It takes layers' `zIndex` property into an account;
// * It does not stop algorithm if some touch is outside the view
// which does not have `clipToBounds` enabled.
// * Taking `layer.zIndex` field into an account is not required because
// lists of `ShadowView`s are already sorted based on `zIndex` prop.

if (!self.userInteractionEnabled || self.hidden || self.alpha < 0.01) {
return nil;
Expand All @@ -294,14 +295,7 @@ - (UIView *)betterHitTest:(CGPoint)point withEvent:(UIEvent *)event
return nil;
}

NSArray<__kindof UIView *> *sortedSubviews =
[self.subviews sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {
// Ensure sorting is stable by treating equal `zIndex` as ascending so
// that original order is preserved.
return a.layer.zPosition > b.layer.zPosition ? NSOrderedDescending : NSOrderedAscending;
}];

for (UIView *subview in [sortedSubviews reverseObjectEnumerator]) {
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
UIView *hitView = [subview hitTest:[subview convertPoint:point fromView:self] withEvent:event];
if (hitView) {
return hitView;
Expand Down

0 comments on commit 65d6f5a

Please sign in to comment.