FS.O5
FS.O5

Reputation: 297

CAShapeLayer isn't visible

I have a subclass of UIView connected to a XIB file of a UIViewController.

I'm using PocketSVG to convert my SVG file to CGPath like that:

override func awakeFromNib() {
        let myPath = PocketSVG.pathFromSVGFileNamed("").takeUnretainedValue()

        let myShapeLayer = CAShapeLayer()
        myShapeLayer.path = myPath

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

        self.layer.addSublayer(myShapeLayer)
    }

the problem is that when I run the app I can't see anything, the layer isn't visible.

What am I doing wrong now?

Thank you!

Upvotes: 1

Views: 367

Answers (1)

Rob
Rob

Reputation: 437432

Given what you reported for CGPathGetPathBoundingBox, you may want to transform the path so it falls within the visible portion of the window.

For example, if you want to translate and scale this so it fits the view:

let insetRect = CGRectInset(bounds, lineWidth / 2.0, lineWidth / 2.0)
let boundingRect = CGPathGetPathBoundingBox(myPath)
let scale = min(insetRect.size.height / boundingRect.size.height, 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)

There are lots of permutations on this idea, but hopefully it illustrates the concept.

Upvotes: 3

Related Questions