简体   繁体   English

如何在不使用 UIWebView 的情况下在 iOS 中显示动画 svg 文件

[英]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.

相关问题 如何将UIWebView中显示的SVG文件的元素居中? - How can I center an element of an SVG file displayed in a UIWebView? 在没有UIWebView的iOS设备上显示.RTF文件-在iOS上是否为initWithRTF? - Display .RTF file on iOS devices WITHOUT UIWebView - initWithRTF on iOS? 我可以使用动画 SVG 作为应用程序的加载图标吗 (Apple iOS) - Can I use an animated SVG for a loading icon for an App (Apple iOS) 如何在ios中阅读pdf文件。我没有使用UIWebView - How to read pdf file in ios .I am not using UIWebView 如何在iOS中使用AFNetworking将网页加载到UIWebView - How can I load a web page to UIWebView using AFNetworking in ios 如何在不使用UIWebView的情况下在iOS 10中显示带有HTML标签的文本(使用Swift) - How to display text with HTML Tags in iOS 10 without using UIWebView (using swift) 不使用UIWebView,如何获取标题和网站说明? - How can I get title, and description of website without using UIWebView? iOS UIWebView中的GIF动画 - Animated gifs in iOS UIWebView 如何在iOS中的UIWebview中获取内容 - How can i get the contents in UIWebview in ios 如何在iOS React Native应用中显示从相机胶卷加载的动画GIF - How can I display an animated GIF loaded from the camera roll in an iOS React Native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM