1

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)")
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
potato
  • 4,479
  • 7
  • 42
  • 99

2 Answers2

3

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?

Community
  • 1
  • 1
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0

You have to get the Shape layer size from the path and set it as layer bounds and then layer bounds as layer frame.

shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake(40, 40, 100, 100)).CGPath
let pathBounds = shapeLayer.path.boundingBoxOfPath
let shapeFrame = CGRect(x: pathBounds.origin.x , y: pathBounds.origin.y, width: 
pathBounds.size.width, height: pathBounds.size.height)
shapeLayer.bounds = shapeFrame
shapeLayer.frame  = shapeLayer.bounds
Imran
  • 3,045
  • 2
  • 22
  • 33