CALayer未被IBDesignable绘制

时间:2016-08-31 10:00:17

标签: swift xcode7 xib calayer

我是新手使用XIB并尝试创建可重用的XIB对象。我有一个简单的XIB,并将XIB视图链接到自定义类:

import UIKit

@IBDesignable

class DrawGuage: UIView {

    //*******************************************************************************************************************//
    //MARK: - CAShapelayes - Background and Foreground
    //*******************************************************************************************************************//
    var backgroundLayer : CALayer!
    var foregroundLayer : CALayer!

    var baseCircle      : CAShapeLayer!


    //*******************************************************************************************************************//
    //MARK: - IBInspectables
    //*******************************************************************************************************************//
    @IBInspectable var backgroundLayerColor : UIColor = UIColor.clearColor()
    @IBInspectable var foregroundLayerColor : UIColor = UIColor.clearColor()

    @IBInspectable var lineWidth: CGFloat = 1.0


    //*******************************************************************************************************************//
    //MARK: - Initiate
    //*******************************************************************************************************************//

    required override init(frame: CGRect) {
        super.init(frame: frame)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }

    override func drawLayer(layer: CALayer, inContext ctx: CGContext) {

        backgroundLayer = CALayer()
        backgroundLayer.frame = self.bounds

        baseCircle = aCircle(backgroundLayer, name: "baseCircle", color: UIColor.blueColor(), lineWidth: 10, radPercent: 0.75)

        backgroundLayer.addSublayer(baseCircle)

        self.layer.addSublayer(backgroundLayer)
    }

    //*******************************************************************************************************************//
    //MARK: - Darwing Functions
    //*******************************************************************************************************************//

    func aCircle(layer: CALayer, name: String, color : UIColor, lineWidth: CGFloat, radPercent: CGFloat) -> CAShapeLayer {

        let shape = CAShapeLayer()
        let path = UIBezierPath()
        let radius : CGFloat = min(CGRectGetMidX(layer.bounds), CGRectGetMidY(layer.bounds)) * radPercent
        let centre : CGPoint = CGPointMake(CGRectGetMidX(layer.bounds), CGRectGetMidY(layer.bounds))

        path.addArcWithCenter(centre, radius: radius, startAngle: CGFloat(-M_PI_2), endAngle: CGFloat(M_PI_2*3), clockwise: true)

        shape.path             = path.CGPath
        shape.fillColor        = UIColor.clearColor().CGColor
        shape.strokeColor      = color.CGColor
        shape.lineWidth        = radius * lineWidth

        shape.name             = name

        return shape

    }

}

这应该在IB中绘制一个简单的空蓝色圆圈,但没有任何内容可见。有什么帮助吗?

Example of output

0 个答案:

没有答案