我如何通过@designable在故事板上看到这个设计

How can i see with @designable this design on storyboard

我正在使用遮罩。

我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var firstImageView: UIImageView!
    @IBOutlet weak var seconImageView: UIImageView!


    let maskView = UIImageView()
    let maskView2 = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()

        maskView.image = UIImage(named: "Up")
        maskView2.image = UIImage(named: "Down")
        firstImageView.mask = maskView
        seconImageView.mask = maskView2
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        maskView.frame = firstImageView.bounds
        maskView2.frame = seconImageView.bounds
    }
}

UIImageView代码:

UIImageViewclass。我看不到这个设计。我能怎么做 ?我添加了自定义 class 但没有用

@IBDesignable
class FirstImageView: UIImageView {

    var maskkView: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutSubviews()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        layoutSubviews()
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        maskkView.image = UIImage(named: "mask")
        mask = maskkView
        maskkView.frame = bounds
    }

}

故事板:

模拟器:

如何使用@designable 在故事板上看到这个设计。

I added custom class but didn't work

我向您保证 IBDesignable UIImageView 的 layoutSubviews 中的遮罩确实有效。示例:

@IBDesignable
class MyImageView : UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        let lay = CAShapeLayer()
        lay.frame = self.bounds
        lay.path = UIBezierPath(ovalIn: self.bounds).cgPath
        self.layer.mask = lay
    }
}

结果:

怀疑您的代码存在问题,因为您正试图在 IBDesignable 代码中使用 UIImage(named:) 从捆绑包中加载图像;我猜那是行不通的。 IBDesignable 代码只是一些在故事板渲染时运行的东西;它不能表现得像真正的 运行 应用程序。