gpt4 book ai didi

ios - drawRect 没有绘制正确的形状

转载 作者:行者123 更新时间:2023-11-30 13:10:55 26 4
gpt4 key购买 nike

我有一个名为 TestViewUIView 子类,它重写了 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 上,我将此 View 作为子类添加到 View 中,如下所示:

let testView = TestView(frame: CGRectMake(0,0, 100, 100))
self.view.addSubview(testView)

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

我在这里做错了什么?

最佳答案

您正在执行 UIBezierPath(ovalInRect: rect)rect 为 100 x 100。所以我期望是一个圆圈。

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

就为什么您没有看到其他内容而言,您的路径远远超出了 100 x 100 尺寸,因此我不确定您期望它呈现什么。如果您希望在 View 中渲染此路径,您应该调整这些坐标以使其落在 TestView 的大小范围内。

关于ios - drawRect 没有绘制正确的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38749187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com