【发布时间】:2020-01-27 18:45:46
【问题描述】:
我在 Stack Overflow 上看到了很多答案,但没有一个对我有帮助。
我创建了一个可以绘制的 DrawView。我创建了一个UIBezierPath 的实例用于绘图。
我想将我的绘图转移到一个小视图(图像上的红色视图)。我就是这样做的:
// This function is called when the user has finished to draw.
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
//drawingView is the red view where I want to see my drawing in the center. This line of code doesn't work like I expect because the drawing doesn't appear in the center of the view.
shapeLayer.position = CGPoint(x: drawingView.frame.midX, y: drawingView.frame.midY)
//I add the shapeLayer to the drawingView.
drawingView.layer.addSublayer(shapeLayer)
}
这是应用程序的图片:
有什么提示吗?谢谢。
【问题讨论】:
标签: swift xcode uiview calayer cgpath