drawRect没有绘制正确的形状

时间:2016-08-03 16:34:53

标签: ios iphone swift cocoa-touch uiview

我有一个名为UIView的{​​{1}}子类,其覆盖范围为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

但它唯一能画出一个白色圆圈而不是它应该绘制的形状。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

你正在UIBezierPath(ovalInRect: rect)rect为100 x 100.所以我希望有一个圆圈。

如果您不想绘制圆圈,请改为使用UIBezierPath()初始化路径。

就你没有看到任何其他东西的原因而言,你的路径远远超出了100 x 100的尺寸,所以我不确定你想要渲染的是什么。如果您希望它在视图中呈现此路径,则应将这些坐标调整为TestView的大小。