drawRect 没有绘制正确的形状

Posted

技术标签:

【中文标题】drawRect 没有绘制正确的形状【英文标题】:drawRect doesn't draws the right shape 【发布时间】:2016-08-03 16:34:52 【问题描述】:

我有一个名为 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 上,我将此视图作为子类添加到这样的视图中:

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

但它唯一会画一个白色圆圈而不是它应该画的形状。

我在这里做错了什么?

【问题讨论】:

【参考方案1】:

您正在使用 UIBezierPath(ovalInRect: rect)rect 为 100 x 100。所以我希望是一个圆圈。

如果你不想画圆,用UIBezierPath()初始化路径。

就您看不到任何其他内容的原因而言,您的路径远远超出了 100 x 100 的大小,所以我不确定您期望它呈现什么。如果您希望 TestView 在视图中呈现此路径,则应将这些坐标调整到其大小范围内。

【讨论】:

我已经尝试过UIBezierPath(),但它没有显示任何内容 是的,您的所有坐标都远大于 100(您的视图的最大宽度和高度),所以我再次不确定您希望它呈现什么。将这些值缩放到 TestView 的大小范围内。

以上是关于drawRect 没有绘制正确的形状的主要内容,如果未能解决你的问题,请参考以下文章

快速绘制矩形形状周围的黑框

drawRect中的UILabel不绘制

触发drawRect重绘视图的按钮

如何在 Java 中绘制三叶草?

使用 CGContextScaleCTM 时避免拉伸笔画

绘制两个形状的共同轮廓,每个形状在不同的 UIView - ObjC