Optimize hit testing.

This commit is contained in:
Mikael Sand
2018-02-23 00:19:10 +02:00
parent db8f1da46b
commit cd43fc151e
13 changed files with 143 additions and 114 deletions
+7 -10
View File
@@ -109,25 +109,21 @@
return (CGPathRef)CFAutorelease(path);
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event withTransform:(CGAffineTransform)transform
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitSelf = [super hitTest:point withEvent:event withTransform:transform];
CGPoint transformed = CGPointApplyAffineTransform(point, CGAffineTransformInvert(self.matrix));
UIView *hitSelf = [super hitTest:transformed withEvent:event];
if (hitSelf) {
return hitSelf;
}
CGAffineTransform matrix = CGAffineTransformConcat(self.matrix, transform);
CGPathRef clip = [self getClipPath];
if (clip) {
CGPathRef transformedClipPath = CGPathCreateCopyByTransformingPath(clip, &matrix);
BOOL insideClipPath = CGPathContainsPoint(clip, nil, point, self.clipRule == kRNSVGCGFCRuleEvenodd);
CGPathRelease(transformedClipPath);
BOOL insideClipPath = CGPathContainsPoint(clip, nil, transformed, self.clipRule == kRNSVGCGFCRuleEvenodd);
if (!insideClipPath) {
return nil;
}
}
for (RNSVGNode *node in [self.subviews reverseObjectEnumerator]) {
@@ -141,13 +137,14 @@
return node;
}
UIView *hitChild = [node hitTest: point withEvent:event withTransform:matrix];
UIView *hitChild = [node hitTest:transformed withEvent:event];
if (hitChild) {
node.active = YES;
return (node.responsible || (node != hitChild)) ? hitChild : self;
}
}
return nil;
}
+4 -1
View File
@@ -17,6 +17,7 @@
NSMutableDictionary<NSString *, RNSVGNode *> *_templates;
NSMutableDictionary<NSString *, RNSVGPainter *> *_painters;
CGAffineTransform _viewBoxTransform;
CGAffineTransform _invviewBoxTransform;
}
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
@@ -129,6 +130,7 @@
eRect:rect
align:self.align
meetOrSlice:self.meetOrSlice];
_invviewBoxTransform = CGAffineTransformInvert(_viewBoxTransform);
CGContextConcatCTM(context, _viewBoxTransform);
}
@@ -167,6 +169,7 @@
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (self.align) {
CGPoint transformed = CGPointApplyAffineTransform(point, _invviewBoxTransform);
for (RNSVGNode *node in [self.subviews reverseObjectEnumerator]) {
if (![node isKindOfClass:[RNSVGNode class]]) {
continue;
@@ -178,7 +181,7 @@
return node;
}
UIView *hitChild = [node hitTest: point withEvent:event withTransform:_viewBoxTransform];
UIView *hitChild = [node hitTest:transformed withEvent:event];
if (hitChild) {
node.active = YES;