gpt4 book ai didi

iphone - UIWebview 处理 SVG 'on the fly'

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:34 25 4
gpt4 key购买 nike

我想知道如何操作已加载到 UIWebview 中的 SVG 文件。目前我正在将 SVG 文件加载到 HTML 文件中,然后将其加载到 UIWebview 中。我假设您会使用 Javascript 来执行此操作,但不确定具体如何操作。理想情况下,我希望能够在 iPad 应用程序中即时操作 SVG。我知道您可以将代码“注入(inject)”到 UIWebview 中,但我的尝试没有成功。清澈如泥?好吧,也许一些代码会有所帮助。

以下是我如何将 html 文件加载到 View Controller .m 文件内的 UIWebview 中:

- (void)viewDidLoad 
{
[super viewDidLoad];

NSString* path = [[NSBundle mainBundle] pathForResource:@"SVG" ofType:@"html"];
NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

webView = [UIWebView new];
webView.frame = CGRectMake(0, 0, 1024, 768);
webView.dataDetectorTypes = UIDataDetectorTypeAll;
webView.userInteractionEnabled = YES;
[webView loadHTMLString:content baseURL:[[NSBundle mainBundle] bundleURL]];

[self.view addSubview:webView];
[webView release];
}

这是加载到 UIWebview 的 html 文件中的代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SVG</title>
</head>
<body>
<object id="circle" data="Circle.svg" width="250" height="250" type="image/svg+xml"/>
</body>
</html>

...最后是 SVG 文件中的代码:

<svg xmlns="http://www.w3.org/2000/svg">
<circle id="redcircle" cx="200" cy="50" r="50" fill="red" />
</svg>

这会在 UIWebview 中创建一个漂亮的红色小圆圈。现在我希望能够在 View Controller .m 文件中快速操纵圆圈。例如,当用户触摸 iPad 屏幕时,将填充颜色属性更改为绿色。这可能吗?

我希望这一切都有意义。这有点令人费解。但最终我想要做的是在 HTML 框架中使用 SVG 动态创建图形并将其显示在 UIWebview 中。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

您可以在页面首次加载后的任何时候通过将其传递给 webView 上的 stringByEvaluatingJavaScriptFromString 来执行任意 Javascript - 就像响应触摸事件一样。

因此,如果用于查找 SVG 对象并更改颜色的 Javascript 看起来像(YMMV,实际上还没有测试过):

var path=document.getElementById("circle").getSVGDocument().getElementById("redcircle");path.style.setProperty("fill", "#00FF00", "");

你可以执行它:

NSString *string = @"var path=document.getElementById('circle').getSVGDocument().getElementById('redcircle');path.style.setProperty('fill', '#00FF00', '');";
[webView stringByEvaluatingJavaScriptFromString:string];

关于iphone - UIWebview 处理 SVG 'on the fly',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com