我有一个名为UIView
的TestView
子类,该类具有对DrawRect
的覆盖,如下所示:
import Foundation
import UIKit
class TestView: UIView {
override func drawRect(rect: CGRect) {
let rectangle5Color = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)
let rectangle5StrokeColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)
let rectangle5Path = UIBezierPath(ovalInRect: rect)
rectangle5Path.moveToPoint(CGPoint(x: 758, y: 1467))
rectangle5Path.addLineToPoint(CGPoint(x: 748, y: 1467))
rectangle5Path.addLineToPoint(CGPoint(x: 204, y: 1467))
rectangle5Path.addLineToPoint(CGPoint(x: 204, y: 1205))
rectangle5Path.addCurveToPoint(CGPoint(x: 266, y: 1205), controlPoint1: CGPoint(x: 204, y: 1205), controlPoint2: CGPoint(x: 248.33, y: 1205))
rectangle5Path.addCurveToPoint(CGPoint(x: 754, y: 1135), controlPoint1: CGPoint(x: 411.79, y: 1205), controlPoint2: CGPoint(x: 627.68, y: 1135))
rectangle5Path.addLineToPoint(CGPoint(x: 758, y: 1135))
rectangle5Path.addCurveToPoint(CGPoint(x: 1232, y: 1205), controlPoint1: CGPoint(x: 884.32, y: 1135), controlPoint2: CGPoint(x: 1086.21, y: 1205))
rectangle5Path.addCurveToPoint(CGPoint(x: 1296, y: 1205), controlPoint1: CGPoint(x: 1233.67, y: 1205), controlPoint2: CGPoint(x: 1296, y: 1205))
rectangle5Path.addLineToPoint(CGPoint(x: 1296, y: 1467))
rectangle5Path.addLineToPoint(CGPoint(x: 758, y: 1467))
rectangle5Path.closePath()
rectangle5Path.moveToPoint(CGPoint(x: 752.5, y: 1009))
rectangle5Path.addCurveToPoint(CGPoint(x: 867, y: 1122.5), controlPoint1: CGPoint(x: 815.74, y: 1009), controlPoint2: CGPoint(x: 867, y: 1059.82))
rectangle5Path.addCurveToPoint(CGPoint(x: 752.5, y: 1236), controlPoint1: CGPoint(x: 867, y: 1185.18), controlPoint2: CGPoint(x: 815.74, y: 1236))
rectangle5Path.addCurveToPoint(CGPoint(x: 638, y: 1122.5), controlPoint1: CGPoint(x: 689.26, y: 1236), controlPoint2: CGPoint(x: 638, y: 1185.18))
rectangle5Path.addCurveToPoint(CGPoint(x: 752.5, y: 1009), controlPoint1: CGPoint(x: 638, y: 1059.82), controlPoint2: CGPoint(x: 689.26, y: 1009))
rectangle5Path.closePath()
rectangle5Color.setFill()
rectangle5Path.fill()
rectangle5StrokeColor.setStroke()
rectangle5Path.lineWidth = 5
rectangle5Path.stroke()
}
}
在我的
ViewController
上,我将此视图作为子类添加到该视图中,如下所示:let testView = TestView(frame: CGRectMake(0,0, 100, 100))
self.view.addSubview(testView)
但唯一会绘制白色圆圈而不是它应绘制的形状的东西。
我在这里做错了什么?
最佳答案
您正在执行UIBezierPath(ovalInRect: rect)
,且rect
为100 x100。所以我希望有一个圆圈。
如果您不想画圆,请改用UIBezierPath()
初始化路径。
关于为什么看不到其他内容的问题,您的路径远远超出了100 x 100的尺寸,因此我不确定您希望它呈现什么。如果希望它在视图中呈现此路径,则应将这些坐标调整为在TestView
的范围内。
关于ios - drawRect无法绘制正确的形状,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38749187/