refactor Use and Defs element with native code support(iOS)

This commit is contained in:
Horcrux
2016-07-19 23:09:51 +08:00
parent 4eddfc6885
commit dd6cb80e84
37 changed files with 535 additions and 282 deletions
-1
View File
@@ -13,7 +13,6 @@
#import "RNSVGSvgView.h"
@interface RNSVGClipPath : RNSVGGroup
@property (nonatomic, strong) NSString *name;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
+10 -3
View File
@@ -10,16 +10,23 @@
@implementation RNSVGClipPath
- (void)renderLayerTo:(CGContextRef)context
- (void)saveDefination:(CGContextRef)context
{
[[self getSvgView] defineClipPath:[self getPath:context] clipPathRef:self.name];
}
// hitTest delagate
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
- (void)removeDefination
{
if (self.name) {
[[self getSvgView] removeClipPath: self.name];
}
}
@end
+21
View File
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNSVGNode.h"
/**
* RNSVG defination are implemented as abstract UIViews for all elements inside Defs.
*/
@interface RNSVGDefination : RNSVGNode
- (void)renderTo:(CGContextRef)context;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
@end
+27
View File
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNSVGDefination.h"
@class RNSVGNode;
@implementation RNSVGDefination
- (void)renderTo:(CGContextRef)context
{
for (RNSVGNode *node in self.subviews) {
[node saveDefination: context];
}
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
@end
+25 -2
View File
@@ -7,6 +7,7 @@
*/
#import "RNSVGGroup.h"
#import <objc/runtime.h>
@implementation RNSVGGroup
@@ -35,17 +36,39 @@
}
// hitTest delagate
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
for (RNSVGNode *node in [self.subviews reverseObjectEnumerator]) {
UIView *view = [node hitTest: point withEvent:event];
if (view != NULL) {
if (view) {
return view;
}
}
return nil;
}
- (void)saveDefination:(CGContextRef)context
{
if (self.name) {
RNSVGSvgView* svg = [self getSvgView];
[svg defineTemplate:self templateRef:self.name];
}
for (RNSVGNode *node in self.subviews) {
[node saveDefination:context];
}
}
- (void)willRemoveSubview:(UIView *)subview
{
[super willRemoveSubview:subview];
}
- (void)mergeProperties:(__kindof RNSVGNode *)target
{
for (RNSVGNode *node in self.subviews) {
[node mergeProperties:target];
}
}
@end
+9 -1
View File
@@ -10,6 +10,8 @@
#import "RNSVGContainer.h"
@class RNSVGNode;
@interface RNSVGSvgView : UIView <RNSVGContainer>
@property (nonatomic, assign) BOOL responsible;
@@ -17,10 +19,16 @@
/**
* define <ClipPath></ClipPath> content as clipPath template.
*/
- (void)defineClipPath:(CGPathRef)clipPath clipPathRef:(NSString *)clipPathId;
- (void)defineClipPath:(CGPathRef)clipPath clipPathRef:(NSString *)clipPathRef;
- (void)removeClipPath:(NSString *)clipPathRef;
- (CGPathRef)getDefinedClipPath:(NSString *)clipPathRef;
- (void)defineTemplate:(RNSVGNode *)template templateRef:(NSString *)templateRef;
- (void)removeTemplate:(NSString *)tempalteRef;
- (RNSVGNode *)getDefinedTemplate:(NSString *)tempalteRef;
@end
+27 -5
View File
@@ -13,7 +13,8 @@
@implementation RNSVGSvgView
{
NSMutableDictionary *clipPaths;
NSMutableDictionary<NSString *, NSValue *> *clipPaths;
NSMutableDictionary<NSString *, RNSVGNode *> *templates;
}
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
@@ -42,8 +43,9 @@
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
for (RNSVGNode *node in self.subviews) {
[node saveDefination:context];
[node renderTo:context];
if (node.responsible && !self.responsible) {
@@ -64,7 +66,7 @@
- (void)defineClipPath:(CGPathRef)clipPath clipPathRef:(NSString *)clipPathRef
{
if (clipPaths == NULL) {
if (!clipPaths) {
clipPaths = [[NSMutableDictionary alloc] init];
}
[clipPaths setValue:[NSValue valueWithPointer:clipPath] forKey:clipPathRef];
@@ -72,14 +74,34 @@
- (void)removeClipPath:(NSString *)clipPathRef
{
if (clipPaths != NULL) {
if (clipPaths) {
[clipPaths removeObjectForKey:clipPathRef];
}
}
- (CGPathRef)getDefinedClipPath:(NSString *)clipPathRef
{
return [[clipPaths valueForKey:clipPathRef] pointerValue];
return clipPaths ? [[clipPaths valueForKey:clipPathRef] pointerValue] : nil;
}
- (void)defineTemplate:(RNSVGNode *)template templateRef:(NSString *)templateRef
{
if (!templates) {
templates = [[NSMutableDictionary alloc] init];
}
[templates setObject:template forKey:templateRef];
}
- (void)removeTemplate:(NSString *)tempalteRef
{
if (templates) {
[templates removeObjectForKey:tempalteRef];
}
}
- (RNSVGNode *)getDefinedTemplate:(NSString *)tempalteRef
{
return templates ? [templates objectForKey:tempalteRef] : nil;
}
@end
+19
View File
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNSVGRenderable.h"
/**
* RNSVG defination are implemented as abstract UIViews for all elements inside Defs.
*/
@interface RNSVGUse : RNSVGRenderable
@property (nonatomic, strong) NSString *href;
@end
+26
View File
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNSVGUse.h"
#import "RCTLog.h"
@implementation RNSVGUse
- (void)renderLayerTo:(CGContextRef)context
{
RNSVGNode* template = [[self getSvgView] getDefinedTemplate:self.href];
if (template) {
[template mergeProperties:self];
[template renderTo:context];
} else if (self.href) {
// TODO: calling yellow box here
RCTLogWarn(@"`Use` element expected a pre-defined svg template as `href` prop, template named: %@ is not defined.", self.href);
}
}
@end