可见图层(CAShapeLayer)框架为零 - Swift

时间:2015-04-27 07:06:15

标签: ios swift frame layer

我正在尝试在我的图层变换时追踪帧中的变化。动画是可见的但不知何故我的frame属性在动画之前和之后设置为(0,0,0,0)。我没有明确设置任何框架属性,但我认为它是基于BezierPath设置的,因为你知道......图层是可见的并且工作所以它的框架不应该是(0,0,0,0)。那是怎么回事?

//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)")

2 个答案:

答案 0 :(得分:3)

我认为masksToBounds属性设置为false,因此图层的框架是什么并不重要。另见: What UIView layer.masksToBounds is doing if set to YES?

答案 1 :(得分:0)

您必须从路径中获取 Shape 图层大小并将其设置为图层边界,然后将图层边界设置为图层框架。

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