使用Playgrounds的Swift中的BezierPath

时间:2016-05-02 18:40:16

标签: ios swift

我是否必须创建自定义类才能在Swift游乐场中使用BezierPath?

以下代码仅显示黑色背景:

import UIKit
import XCPlayground

class GraphView : UIView {

    override func drawRect(rect: CGRect) {

        let path = UIBezierPath(rect: rect)
        path.moveToPoint(CGPointMake(0,0))
        path.addLineToPoint(CGPointMake(50,100))
        path.closePath()
        UIColor.redColor().setFill()

        path.stroke()
    }

}


let graphView = GraphView(frame: CGRectMake(0,0,960,640))

2 个答案:

答案 0 :(得分:5)

您必须在代码末尾使用此功能:

ArrayList

并打开游乐场"助理编辑"看到结果。

还可以更改视图的背景颜色,因为它是黑色的...而且你的线也是黑色的。 ;)

答案 1 :(得分:1)

在Swift 5中:

import UIKit

class GraphView : UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(rect: rect)
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 50, y: 100))
        path.stroke()
    }

}

let graphView = GraphView(frame: CGRect(x: 0, y: 0, width: 960, height: 640))

screenshot of view in playground Swift 5.2,Xcode 11.4