构建后自动填充偶尔失败,并且不起作用(快速)

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

我离开游戏太久了。任何人都知道这里发生了什么吗?

这里是我正在观看的YouTube video,供参考。

Check errors here

这里是代码:

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 drawing
1个回答
0
投票

方法签名已更改,在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()
        }
    }
}

已在自定义类中配置了视图,则可以在情节提要中为视图设置此类:enter image description here

因此,您将看到:

enter image description here

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