我离开游戏太久了。任何人都知道这里发生了什么吗?
这里是我正在观看的YouTube video,供参考。
这里是代码:
import UIKit
class DrawExamples: UIView {
override func drawRect(rect: CGRect) {
// context is the object used for drawing
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 3.0)
CGContextSetStrokeColorWithColor(context, UIColor.purpleColor().CGColor)
/*
//straight line
CGContextMoveToPoint(context, 30, 30)
CGContextAddLineToPoint(context, 150, 320)
*/
CGContextMoveToPoint(context, 50, 50)
CGContextAddLineToPoint(context, 90, 130)
CGContextAddLineToPoint(context, 180, 100)
CGContextAddLineToPoint(context, 90, 60)
CGContextAddLineToPoint(context, 90, 130)
//Actually draw the path
CGContextStrokePath(context)
}
}
方法签名已更改,在Swift 5中,您可以使用以下代码:
class DrawExamples: UIView {
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
context.setLineWidth(3.0)
context.setStrokeColor(UIColor.purple.cgColor)
context.move(to: CGPoint(x: 50, y: 50))
context.addLine(to: CGPoint(x: 90, y: 130))
context.addLine(to: CGPoint(x: 180, y: 100))
context.addLine(to: CGPoint(x: 90, y: 60))
context.addLine(to: CGPoint(x: 90, y: 130))
context.strokePath()
}
}
}
已在自定义类中配置了视图,则可以在情节提要中为视图设置此类:
因此,您将看到: