我怎样才能在情节提要中看到绘图?

How can i see the drawing in storyboard?

我画了三角形,但在故事板上看不到。我怎样才能在情节提要上看到这张图?

查看控制器代码:

class ViewController: UIViewController {

    @IBOutlet weak var fooView: UIView!

    var polygonShape = CAShapeLayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        let polygonPath = UIBezierPath()
        polygonPath.move(to: CGPoint(x: 50, y: -1))
        polygonPath.addLine(to: CGPoint(x: 93.3, y: 74))
        polygonPath.addLine(to: CGPoint(x: 6.7, y: 74))
        polygonPath.close()

        polygonShape.frame = fooView.bounds
        polygonShape.path = polygonPath.cgPath

        fooView.layer.mask = polygonShape

    }
}

将您的 class 更改为:

@IBDesignable
class FirstImageView: UIView {

    var polygonShape: CAShapeLayer!

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

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

    func commonInit() -> Void {

        if polygonShape == nil {
            polygonShape = CAShapeLayer()
            layer.mask = polygonShape
        }

    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let polygonPath = UIBezierPath()
        polygonPath.move(to: CGPoint(x: 0.0, y: bounds.size.height))
        polygonPath.addLine(to: CGPoint(x: bounds.width * 0.5, y: 0.0))
        polygonPath.addLine(to: CGPoint(x: bounds.size.width, y: bounds.size.height))
        polygonPath.close()

        polygonShape.path = polygonPath.cgPath
    }

}

您可以从 ViewControllerclass 中删除 所有。此时您甚至根本不需要 class。

Select 故事板上的视图,并在 Identity Inspector 窗格的顶部将 Custom Class 从默认的 UIView 更改为 FirstImageView

在顶部 Editor 菜单下,select Refresh All Views 或确保选中 Automatically Refresh Views

结果: