run hitTest only if necessary

This commit is contained in:
Horcrux
2016-05-18 20:15:29 +08:00
parent bf5d6c8a8b
commit f34c0c209a
10 changed files with 51 additions and 7 deletions
+3
View File
@@ -11,9 +11,12 @@
#import "RNSVGContainer.h"
#import "RNSVGNode.h"
#import "RNSVGCGFCRule.h"
#import "RNSVGSvgView.h"
@interface RNSVGGroup : RNSVGNode <RNSVGContainer>
@property (nonatomic, strong) NSString *asClipPath; // Current group is a <ClipPath /> element and asClipPath is its id.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (RNSVGSvgView *)getSvgView;
@end
+16 -2
View File
@@ -13,9 +13,15 @@
- (void)renderLayerTo:(CGContextRef)context
{
if (self.asClipPath == NULL) {
RNSVGSvgView* svg = [self getSvgView];
[self clip:context];
for (RNSVGNode *node in self.subviews) {
[node renderTo:context];
if (node.touchable && !svg.touchable) {
self.touchable = YES;
}
}
} else {
[self defineClipPath:[self getPath:context] clipPathId:self.asClipPath];
@@ -29,7 +35,6 @@
CGAffineTransform transform = node.transform;
CGPathAddPath(path, &transform, [node getPath:context]);
}
return path;
}
@@ -39,7 +44,6 @@
{
for (RNSVGNode *node in [self.subviews reverseObjectEnumerator]) {
UIView *view = [node hitTest: point withEvent:event];
if (view != NULL) {
return view;
}
@@ -47,4 +51,14 @@
return nil;
}
- (RNSVGSvgView *)getSvgView
{
UIView *parent = self.superview;
while ([parent class] != [RNSVGSvgView class]) {
parent = parent.superview;
}
return (RNSVGSvgView *)parent;
}
@end
+3
View File
@@ -11,4 +11,7 @@
#import "RNSVGContainer.h"
@interface RNSVGSvgView : UIView <RNSVGContainer>
@property (nonatomic, assign) BOOL touchable;
@end
+10
View File
@@ -21,8 +21,13 @@
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
for (RNSVGNode *node in self.subviews) {
[node renderTo:context];
if (node.touchable && !self.touchable) {
self.touchable = YES;
}
}
}
@@ -31,4 +36,9 @@
self.backgroundColor = inheritedBackgroundColor;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return self.touchable ? [super hitTest:point withEvent:event] : nil;
}
@end
+2
View File
@@ -23,6 +23,7 @@
@property (nonatomic, assign) RNSVGCGFCRule clipRule;
@property (nonatomic, assign) CGPathRef clipPath; // convert clipPath="M0,0 L0,10 L10,10z" into path
@property (nonatomic, strong) NSString *clipPathId; // use clipPath="url(#clip)" as ClipPath
@property (nonatomic, assign) BOOL touchable;
- (void)invalidate;
- (void)renderTo:(CGContextRef)context;
@@ -53,4 +54,5 @@
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
@end
-1
View File
@@ -7,7 +7,6 @@
*/
#import "RNSVGNode.h"
#import "RNSVGContainer.h"
static NSMutableDictionary *ClipPaths;
+1
View File
@@ -32,5 +32,6 @@ RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(opacity, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(trans, CGAffineTransform)
RCT_EXPORT_VIEW_PROPERTY(clipPathId, NSString)
RCT_EXPORT_VIEW_PROPERTY(touchable, BOOL)
@end