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

时间:2017-09-20 01:25:25

标签: ios ibdesignable

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

导入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

2 个答案:

答案 0 :(得分:0)

应该是这样的:

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() {

    }
}

答案 1 :(得分:0)

试一下

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
    }
}