我如何在情节提要中看到绘图?

问题描述 投票:0回答:1

我绘制了三角形,但在情节提要板上看不到。我如何在情节提要板上看到此图?

enter image description here查看控制器代码:

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

    }
}
ios swift xcode uibezierpath cashapelayer
1个回答
2
投票

将您的课程更改为此:

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

}

您可以从您的ViewController类中删除everything >>] >>。您现在甚至根本不需要该课程。

选择情节提要上的视图,然后在Identity Inspector窗格的顶部,将Custom Class从默认的UIView更改为FirstImageView

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

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.