【问题标题】:Visible layer's (CAShapeLayer) frame is zero - Swift可见层的(CAShapeLayer)框架为零 - Swift
【发布时间】:2015-04-27 07:06:15
【问题描述】:

我正在尝试在我的图层转换时追踪帧中的变化。动画是可见的,但不知何故,我的 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)")

【问题讨论】:

    标签: ios swift frame layer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您必须从路径中获取 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多