【发布时间】:2020-01-27 20:05:25
【问题描述】:
我创建了一个可以绘制的 DrawView。我创建了一个 UIBezierPath 实例用于绘图。
// This function is called when the user has finished drawing.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let shapeLayer = CAShapeLayer()
//The CAShapeLayer has the same path of the drawing (currentPath is the instance of UIBezierPath).
shapeLayer.path = currentPath?.cgPath
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 10.0
//Here I set the shapeLayer on the drawingView (the blue one that you can see in the image)
drawingView.layer.addSublayer(shapeLayer)
shapeLayer.position = CGPoint(x: drawingView.layer.bounds.midX, y: drawingView.layer.bounds.midY)
shapeLayer.backgroundColor = UIColor.blue.cgColor
shapeLayer.frame = drawingView.layer.bounds
}
问题是路径(例如图像中的数字 3)没有以它的 shapeLayer 为中心。
shapeLayer.path?.boundingBoxOfPath = (289.5, 349.5, 525.0, 129.0)
shapeLayer.frame = (0.0, 0.0, 200.0, 200.0)
shapeLayer.position = (100.0, 100.0)
drawingView.frame = (0.0, 912.0, 200.0, 200.0)
有什么提示吗?谢谢
【问题讨论】:
-
我设置shapeLayer的frame等于drawingView的frame,但是还是不行
-
drawingView的尺寸和shapeLayer一样
-
试试
shapeLayer.position = drawingView.center我习惯绘制中心在.zero 的形状。 -
谢谢,但不幸的是,如果我写了那个代码,shapeLayer 甚至都不会出现
-
@LeoDabus 我已经告诉他了,这只是stackoverflow.com/questions/59937082/…的重复@
标签: swift xcode uiview calayer cgpath