我试图通过传递点集来画一个循环中的多个点。但是,不会出现一个点。想法-我正在解析xml文档并提取要在视图上绘制的点。
Edit - I have updated the code with suggested changes and it is working.
//View Class
class DrawTrace: UIView
{
var points = [CGPoint]()
{
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect)
{
let size = CGSize(width: 1, height: 1)
UIColor.white.set()
for point in points
{
print(point.x,point.y)
let dot = UIBezierPath(ovalIn: CGRect(origin: point, size: size))
dot.fill()
}
}
}
//View Controller Class
class ViewController: UIViewController
{
var object : traceDoc = traceDoc()
let trace = DrawTrace(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
override func viewDidLoad()
{
super.viewDidLoad()
object.collectionOfData()
trace.points = object.tracePoints
self.view.addSubview(trace)
}
}
为视图控制器中的视图层次添加了视图实例。创建了DrawTrace实例并附加到tracepoints数组。
答案 0 :(得分:0)
看不见黑视图中的黑色点,请尝试将白色点变成白色:
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.white.cgColor
此外,在UIViewController代码中,您没有将DrawTrace添加到视图层次结构中,请尝试在viewDidLoad中进行操作:
self.view.addSubview(drawTraceInstance)
最后,似乎您必须遍历CGPoint的数组。使用数组的用户FastEnumeration代替了使用跨步:
for point in points
{
drawDot(x: point.x, y: point.y,Color: fillColor, size: size)
}
答案 1 :(得分:0)
您的代码比需要的复杂得多。一个大问题是,每次调用draw
时,您都会添加越来越多的图层。
您无需在自定义视图中使用图层来绘制点。这是一个更简单的解决方案:
class DrawTrace: UIView
{
var points = [CGPoint]() {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect)
{
let size = CGSize(width: 2, height: 2)
UIColor.black.set()
for point in points {
let dot = UIBezierPath(ovalIn: CGRect(origin: point, size: size))
// this just draws the circle by filling it.
// Update any properties of the path as needed.
// Use dot.stroke() if you need an outline around the circle.
dot.fill()
}
}
}
let trace = DrawTrace(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
trace.backgroundColor = .white
trace.points = [ CGPoint(x: 10, y: 10), CGPoint(x: 35, y: 100)]
以上内容可以复制到游乐场。