如何获取SVGElement所属的组?

时间:2013-03-17 16:59:39

标签: ios objective-c svg calayer svgkit

现在我这样做:

NodeList* elementsUsingTagG = [third_floor.DOMDocument getElementsByTagName:@"g"];
auds = [elementsUsingTagG.internalArray objectAtIndex:1];
SVGElement *elem = (SVGElement*)[third_floor.DOMDocument getElementById:hitLayer.name];
if ([auds.childNodes.internalArray containsObject:elem])
{
    [self deselectTappedLayer];
    lastTappedLayer = hitLayer;

    if (lastTappedLayer != nil)
    {
        lastTappedLayer.opacity = 1.0f;
        NSLog(@"Clicked on auditory %@, highlight it!", hitLayer.name);
    }
}

我可以获得一些SVGElement / CALayer / Node的属性,它可以识别这个元素所属的组吗?感谢

1 个答案:

答案 0 :(得分:1)

我猜是这样的。我没有测试过代码,但我希望它可以帮到你。

迭代版本:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    for (node = node.parentNode; node != nil; node = node.parentNode) {
        if (![node isKindOfClass:[Element class]]) {
            continue;
        }

        Element *element = (Element *)node;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return nil;
}

递归版:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    if (node.parentNode == nil) {
        return nil;
    }

    if ([node.parentNode isKindOfClass:[Element class]]) {
        Element *element = (Element *)node.parentNode;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return [self findFirstParentElementFrom:node.parentNode
                                withTagName:tagName];
}

在你的情况下使用其中一个:

Element *groupElement = [self findFirstParentElementFromNode:elem
                                                 withTagName:@"g"];