将CGPath宽度设置为视图宽度

时间:2016-08-04 08:47:00

标签: ios objective-c iphone swift cocoa-touch

我正在使用PocketSVG将我的SVG文件转换为CGPath,就像这样:

override func awakeFromNib() {
        super.awakeFromNib()

        let myPath = PocketSVG.pathFromSVGFileNamed("CategoriesBar").takeUnretainedValue()

        let lineWidth: CGFloat = 3.0

        let insetRect = CGRectInset(bounds, lineWidth / 2.0, lineWidth / 2.0)
        let boundingRect = CGPathGetPathBoundingBox(myPath)
        let scale = insetRect.size.width / boundingRect.size.width
        var transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(-boundingRect.origin.x * scale + insetRect.origin.x, -boundingRect.origin.y * scale + insetRect.origin.y), scale, scale)

        let transformedPath = CGPathCreateMutableCopyByTransformingPath(myPath, &transform)

        let myShapeLayer = CAShapeLayer()
        myShapeLayer.path = transformedPath

        myShapeLayer.strokeColor = UIColor.redColor().CGColor
        myShapeLayer.lineWidth = lineWidth
        myShapeLayer.fillColor = UIColor.whiteColor().CGColor

        self.layer.addSublayer(myShapeLayer)
    }

问题是myShapeLayer视野越大,它被切断了,我希望路径的宽度(我不关心高度)等于查看宽度。

我该怎么做?

谢谢!

0 个答案:

没有答案