使用IBDesignable和IBInspectable预览CAShapeLayer

时间:2017-08-02 21:29:12

标签: swift xcode uikit core-animation ibdesignable

我尝试创建一个包含形状图层的视图,可以在Xcode中预览。虽然大部分都有效,但我还是设法让它完全无法运行。具体来说,我无法弄清楚如何设置形状图层的笔触属性,因为它需要在添加图层之前设置,否则不会渲染笔划。但是,如果我在此之前设置它,则可检查的属性不再起作用。这是我走了多远:

import Foundation
import UIKit

@IBDesignable
class CustomView: UIView
{

    @IBInspectable var layerBackgroundColor: UIColor? {
        didSet {
            if let actualColor = layerBackgroundColor {
                layer.backgroundColor = actualColor.cgColor
            } else {
                layerBackgroundColor = nil
            }
        }
    }

    @IBInspectable var strokeColor: UIColor? = UIColor.red {
        didSet {
            if let actualColor = strokeColor {
                self.shapeLayer.strokeColor = actualColor.cgColor
            } else {
                shapeLayer.strokeColor = nil
            }
        }
    }

    private var shapeLayer: CAShapeLayer

    override func prepareForInterfaceBuilder()
    {
        let width = self.bounds.width
        let height = self.bounds.height

        shapeLayer = CAShapeLayer()
        shapeLayer.frame = CGRect(x: 0, y: 0, width: width, height: height)
        let path = CGMutablePath()
        let middle = width/2
        path.move(to:CGPoint(x:middle, y:20))

        path.addLine(to:CGPoint(x:middle, y:100))

        layerBackgroundColor = UIColor.gray
        // If this is not set no stroke will be rendered!
        strokeColor = UIColor.red
        shapeLayer.path = path
        shapeLayer.fillColor = nil
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineCap = kCALineCapRound
        shapeLayer.lineWidth = 5
        layer.addSublayer(shapeLayer)

    }


    override init(frame: CGRect)
    {
        shapeLayer = CAShapeLayer()
        super.init(frame:frame)
    }

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
}

1 个答案:

答案 0 :(得分:2)

您正在prepareForInterfaceBuilder中创建一个新的shapeLayer,因此您需要在该方法中设置shapeLayer的颜色属性。可检查变量在初始化之后但在prepareForInterfaceBuilder之前分配,因此在prepareForInterfaceBuilder中创建一个新的CAShapeLayer会破坏先前的分配。我假设在实践中你将在另一个方法中为你的类做自定义绘图(因为这个方法永远不会在IB之外调用)。

无论如何,下面的代码应该解决你的IB问题,我做了四处修改,全部评论....

import Foundation
import UIKit

@IBDesignable
class CustomView: UIView
{
    @IBInspectable var layerBackgroundColor: UIColor? {
        didSet {
            if let actualColor = layerBackgroundColor {
                layer.backgroundColor = actualColor.cgColor
            } else {
                layerBackgroundColor = nil
            }
        }
    }

    @IBInspectable var strokeColor: UIColor? {//1 -  Maybe you want to comment out assignment to UIColor.red:   = UIColor.red {
        didSet {
            if let actualColor = strokeColor {
                self.shapeLayer.strokeColor = actualColor.cgColor
            } else {
                shapeLayer.strokeColor = nil
            }
        }
    }

    private var shapeLayer: CAShapeLayer

    override func prepareForInterfaceBuilder()
    {
        let width = self.bounds.width
        let height = self.bounds.height

        shapeLayer = CAShapeLayer()
        shapeLayer.frame = CGRect(x: 0, y: 0, width: width, height: height)
        let path = CGMutablePath()
        let middle = width/2
        path.move(to:CGPoint(x:middle, y:20))

        path.addLine(to:CGPoint(x:middle, y:100))

        //2 - you probably also want to set the shapeLayer's background color to the class variable, not hardcode the class variable
        //layerBackgroundColor = UIColor.gray
        shapeLayer.backgroundColor = layerBackgroundColor?.cgColor


        // If this is not set no stroke will be rendered!
        //3 Comment the hardcoding line out
        //strokeColor = UIColor.red
        //4 - assign the shapeLayer's stroke color
        shapeLayer.strokeColor = strokeColor?.cgColor

        shapeLayer.path = path
        shapeLayer.fillColor = nil
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineCap = kCALineCapRound
        shapeLayer.lineWidth = 5
        layer.addSublayer(shapeLayer)

    }


    override init(frame: CGRect)
    {
        shapeLayer = CAShapeLayer()
        super.init(frame:frame)
    }

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
}
相关问题