【问题标题】:How can i see the drawing in storyboard?如何在情节提要中看到绘图?
【发布时间】:2020-01-27 14:39:33
【问题描述】:

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

查看控制器代码:

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】:

把你的班级改成这样:

@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 类中删除所有内容。此时您甚至根本不需要该课程。

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

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

结果:

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2020-05-14
    相关资源
    最近更新 更多