[英]How can i display animated svg file in iOS without using UIWebView
Currently i am using UIWebView
to display animated svg file in my native iOS application.目前我正在使用UIWebView
在我的原生 iOS 应用程序中显示动画 svg 文件。
This works fine except the CPU usage is constantly being on higher side as long as app is in foreground.这工作正常,除了只要应用程序在前台,CPU 使用率就会一直偏高。
Is there any better way to display svg file without using UIwebView
有没有更好的方法来显示 svg 文件而不使用UIwebView
I have already tried many third party libraries but all works only with static svg file not animated svgs我已经尝试过很多第三方库,但都只适用于 static svg 文件而不是动画 svgs
I have tried following我试过以下
https://github.com/mchoe/SwiftSVG https://github.com/mchoe/SwiftSVG
https://github.com/exyte/Macaw https://github.com/exyte/Macaw
https://github.com/onmyway133/Snowflake https://github.com/onmyway133/Snowflake
https://github.com/SVGKit/SVGKit https://github.com/SVGKit/SVGKit
My app supports iOS 9 and above, My code is in Swift3我的应用支持iOS 9及以上,我的代码在Swift3
Here is the link of on of the svg file that i am trying to display这是我要显示的 svg 文件的链接
http://www.mediafire.com/file/04ojy6t4e3c41lv/03d.svg http://www.mediafire.com/file/04ojy6t4e3c41lv/03d.svg
First install the pod in your podfile首先在你的 podfile 中安装 pod
pod `SVGKit`
Then import the library in your view controller file然后在你的视图中导入库 controller 文件
import SVGKit
Then create SVGKImageObjectView object and add it to your view然后创建 SVGKImageObjectView object 并将其添加到您的视图中
let svgImage = SVGKFastImageView(svgkImage : SVGKImage(contentOf:Bundle.main.url(forResource: "animated", withExtension : "svg")!))
self.view.addSubview(svgImage)
To animate the SVG You can use play function动画 SVG 你可以使用 play function
svgImage.svgKImage.play()
You can also set your frame and layout您还可以设置框架和布局
svgImage.frame = CGRect(x:0, y:0, width:200, height: 200)
svgImage.center = self.view.center
You should take look at SVGKit ! 你应该看看SVGKit !
This library is based on core animation. 该库基于核心动画。
Refer this tutorial , 参考本教程 ,
First install pod or manually install SVGKit into your project. 首先安装pod或手动将SVGKit安装到项目中。
Then drag and drop your svg file in to your project. 然后将svg文件拖放到项目中。
Then you can do something like, 那你可以做点什么,
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(20, 70, 300, 300)];
myView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:myView];
// SVGKImage *image = [SVGKImage imageWithContentsOfFile:@"animated-svg-icon"];
SVGKImage *image = [SVGKImage imageNamed:@"cd-icon.svg"];
[self animateCALayer:image.CALayerTree duration:2.0];
[myView.layer addSublayer:image.CALayerTree];
here cd-icon.svg
is the svg image. 这里cd-icon.svg
是svg图像。 Put all the html,js and css files with it in your bundle! 将所有html,js和css文件放在你的包中! (put whole folder of svg in your project) (将svg的整个文件夹放在项目中)
If you get any error related CocoaLumberjack
then install pod 'CocoaLumberjack'
in your project and replace @import CocoaLumberjack;
如果你得到任何与CocoaLumberjack
相关的CocoaLumberjack
那么在你的项目中安装pod 'CocoaLumberjack'
并替换@import CocoaLumberjack;
with #import "CocoaLumberjack.h"
in SVGKit-prefix.pch
在SVGKit-prefix.pch
使用#import "CocoaLumberjack.h"
Follow everything step by step as I mentioned. 正如我所提到的,一步一步地遵循一切。 And you are done. 你完成了。 You will displaying svg in UIView
instead of webview
. 您将在UIView
显示svg而不是webview
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.