swift4 根据路径绘画SVG

根据上司要求把所有的图片都改成SVG格式的,然后我就在网上找了一些类库,比如说SVGKit类库,网上也有好多它的使用,但是我用的swift,SVGKit是OC的库,这时也有人会说SVGKit也可以在swift项目中使用,但是我搞了一天没成功,可能是我太笨了鄙视,然后我就自己写了一个根据路径串在画板上把这张图画出来,话不多说,直接上代码。

ViewController类中创建一个ImageView,将画出的svgImage放到ImageView上

 

import UIKit

 

class ViewController: UIViewController {

 

    var imageView : UIImageView!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.\

        

         imageView = UIImageView(frame: CGRect(x: 40, y: 70, width: 250, height: 250))

        

        self.view.addSubview(imageView)

    }

 override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

    @IBAction func onClick(_ sender:Any) {

        let image =SVGImage().convertViewToSVGImage(width:imageView.bounds.width,height:imageView.bounds.height,data:"M2,21.625 C2,21.625 30,21.625 30,21.625 31.105,21.625 32,22.52 32,23.625 32,23.625 32,25 32,25 32,26.105 31.105,27 30,27 30,27 2,27 2,27 0.89499998,27 0,26.105 0,25 0,25 0,23.625 0,23.625 0,22.52 0.89499998,21.625 2,21.625 z M2,11.125 C2,11.125 30,11.125 30,11.125 31.105,11.125 32,12.02 32,13.125 32,13.125 32,14.5 32,14.5 32,15.605 31.105,16.5 30,16.5 30,16.5 2,16.5 2,16.5 0.89499998,16.5 0,15.605 0,14.5 0,14.5 0,13.125 0,13.125 0,12.02 0.89499998,11.125 2,11.125 z M2,0 C2,0 30,0 30,0 31.105,0 32,0.89499998 32,2 32,2 32,3.375 32,3.375 32,4.48 31.105,5.375 30,5.375 30,5.375 2,5.375 2,5.375 0.89499998,5.375 0,4.48 0,3.375 0,3.375 0,2 0,2 0,0.89499998 0.89499998,0 2,0 z", lineColor:UIColor.red,fillColor:UIColor,blue)

        imageView.image = image

    }

    

}

 

SVGImage类将DrawSvgPathView画出来的图转成Image

import UIKit

 

open class SVGImage:NSObject {

    

    let svgView : DrawSvgPathView!

    

    public override init() {

        //        super.init()

        svgView = DrawSvgPathView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        

    }

    //将视图转成Image

    open func convertViewToSVGImage(width:CGFloat,height:CGFloat,data:String, lineColor:UIColor, fillColor: UIColor? = UIColor.clear) ->UIImage?{

        

        svgView.layer.sublayers =nil

        svgView.setPathFromSvg(width: width,height: height,data: data, lineColor: lineColor, fillColor: fillColor)

        UIGraphicsBeginImageContextWithOptions(CGSize(width:width,height:height),false,UIScreen.main.scale)

        svgView.layer.render(in:UIGraphicsGetCurrentContext()!)

        let image =UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        

        return image

        

    }

}

 

DrawSvgPathView类根据PocketSVG解析出来的路径绘画图片。

import UIKit

 

 class DrawSvgPathView: UIView {

 

    var _shapeView :CAShapeLayer =CAShapeLayer()

    var _strokeLineColor : UIColor!

    var _strokeFillColor : UIColor!

    

    override init(frame:CGRect) {

        super.init(frame: frame)

    }

    

    required publicinit?(coder aDecoder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    open func setPathFromSvg(width:CGFloat,height:CGFloat,data:String, lineColor:UIColor, fillColor: UIColor? = UIColor.clear){

        

        self.layer.sublayers =nil

        self.bounds.size =CGSize(width: width, height: height)

        self._strokeLineColor = lineColor

        self._strokeFillColor = fillColor

        let pocketSVG = PocketSVG(str: data)

        self._shapeView.path = pocketSVG.bezier.cgPath

        

        self.scale()

        

        

    }

    

    func scale(){ </

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,它可以方便地创建可交互的用户界面。在Vue中,使用SVG路径动画可以实现各种有趣的效果。 首先,要在Vue项目中使用SVG路径动画,需要安装vue-svg路径动画扩展。可以通过运行`npm install vue-svg-path-animation`命令来安装。 一旦安装完成,就可以在Vue组件中使用SVG路径动画了。首先,需要在Vue组件中引入`vue-svg-path-animation`模块。然后,可以在组件的`data`选项中创建一个属性来存储SVG路径。这个属性可以在`<svg>`元素的`d`属性中使用。 接下来,可以使用Vue的生命周期钩子函数来触发SVG路径动画。例如,在`mounted`钩子函数中可以使用`setPath`方法来更新SVG路径属性,从而触发动画。可以在该钩子函数中设置一定的延迟,以便动画效果更加明显。 除了在`mounted`钩子函数中触发动画,还可以根据需要在其他钩子函数中实现动画效果。例如,在`created`钩子函数中初始化SVG路径属性,然后在`beforeDestroy`钩子函数中清除SVG路径属性,以停止动画。 在Vue组件中使用SVG路径动画可以为用户提供一种更加丰富和吸引人的交互体验。通过使用Vue的生命周期钩子函数和`vue-svg-path-animation`扩展,可以轻松地实现各种有趣的SVG路径动画效果。这不仅可以使用户界面更加生动,还可以提高用户对网站或应用程序的参与度和满意度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值