IBDesignable 中用于制作渐变的构建时错误

Buildtime errors in IBDesignable for making gradient

我在尝试使用 IBDesignable 创建自定义渐变选项 tutorial,但即使只是处理教程中的代码,我也会遇到意外错误

导入 UIKit

@IBDesignable
class GradientView: UIView {

    @IBInspectable var FirstColor: UIColor.clear{
        didSet {
            updateView ()
        }
    }
    @IBInspectable var SecondColor: UIColor.clear{
        didSet {
            updateView ()
        }
    }

    @IBInspectable var ThirdColor: UIColor.clear{
        didSet {
            updateView ()
        }
        func updateView() {

        }
    }
}

enter image description here

应该是这样的:

import UIKit

@IBDesignable
class GradientView: UIView {
    @IBInspectable var FirstColor: UIColor = .clear {
        didSet {
            updateView()
        }
    }

    @IBInspectable var SecondColor: UIColor = .clear {
        didSet {
            updateView()
        }
    }

    @IBInspectable var ThirdColor: UIColor = .clear {
        didSet {
            updateView()
        }
    }

    func updateView() {

    }
}

试试这个

import UIKit

@IBDesignable class GradientView: UIView {

    @IBInspectable var topColor: UIColor = UIColor.white {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var bottomColor: UIColor = UIColor.black {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var shadowColor: UIColor = UIColor.black {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var shadowX: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var shadowY: CGFloat = -3 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var shadowBlur: CGFloat = 3 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var startPointX: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var startPointY: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var endPointX: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var endPointY: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            setNeedsLayout()
        }
    }

    override class var layerClass: AnyClass {
        return CAGradientLayer.self
    }

    override func layoutSubviews() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
        gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
        gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
        layer.cornerRadius = cornerRadius
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOffset = CGSize(width: shadowX, height: shadowY)
        layer.shadowRadius = shadowBlur
        layer.shadowOpacity = 1
    }
}