Swift : 循环绘制点
Swift : drawing the dots in loop
我试图通过传递一组点来循环绘制多个点。但是没有一个点被绘制在视图上。想法 - 我正在解析 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 的视图 controller.Created 实例中并附加到跟踪点数组。
黑色视图中的黑点不可见,请尝试将点设为白色:
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.white.cgColor
此外,在 UIViewController 代码中,您没有将 DrawTrace 添加到视图层次结构中,请尝试在 viewDidLoad:
self.view.addSubview(drawTraceInstance)
最后,您似乎必须遍历 CGPoint 数组。用户 FastEnumeration over Array:
而不是使用 stride
for point in points
{
drawDot(x: point.x, y: point.y,Color: fillColor, size: size)
}
您的代码比需要的复杂得多。一个大问题是每次调用 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)]
以上可以复制到playground中。
我试图通过传递一组点来循环绘制多个点。但是没有一个点被绘制在视图上。想法 - 我正在解析 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 的视图 controller.Created 实例中并附加到跟踪点数组。
黑色视图中的黑点不可见,请尝试将点设为白色:
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.white.cgColor
此外,在 UIViewController 代码中,您没有将 DrawTrace 添加到视图层次结构中,请尝试在 viewDidLoad:
self.view.addSubview(drawTraceInstance)
最后,您似乎必须遍历 CGPoint 数组。用户 FastEnumeration over Array:
而不是使用 stridefor point in points
{
drawDot(x: point.x, y: point.y,Color: fillColor, size: size)
}
您的代码比需要的复杂得多。一个大问题是每次调用 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)]
以上可以复制到playground中。