Search code examples
iosswiftframelayer

Visible layer's (CAShapeLayer) frame is zero - Swift


I'm trying to track down the changes in frame while my layer transforms. The animation is visible but somehow my frame property is set to (0,0,0,0) before the animation and after it. I didn't explicitly set any frame property but I assumed it is set based on BezierPath, because you know...the layer is visible and working so it's frame shouldn't be (0,0,0,0). So what's going on?

//in viewDidLoad()
            var shapeLayer: CAShapeLayer!
            shapeLayer = CAShapeLayer()  
            shapeLayer.lineWidth = 2
            shapeLayer.strokeColor = UIColor.redColor().CGColor
            shapeLayer.fillColor = UIColor(red: 0.1, green: 0.3, blue: 0.6, alpha: 0.3).CGColor
            shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake(40, 40, 100, 100)).CGPath
            println("\(shapeLayer.frame)")
            shapeLayer.contentsScale = UIScreen.mainScreen().scale
            view.layer.addSublayer(shapeLayer)

//when button is pressed
        shapeLayer.transform = CATransform3DMakeScale(2, 1, 3)
        println("\(shapeLayer.frame)")

Solution

  • I think the masksToBounds property is set to false, so it doesn't matter what the frame of your layer is. See also here: What UIView layer.masksToBounds is doing if set to YES?