我正在尝试为NSBezierPath添加撤消功能。
此函数使用半径为45.的NSBezierPath绘制圆,其中mouseLocation为圆的中心
然后它在CAShapeLayer上绘制NSBezierPath并添加到NSViewController的视图中。
我应该如何为此方法添加撤消功能。 提前谢谢。
func bezierPathMouseUndoTest(mouseLocation: CGPoint, color: NSColor) {
let frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
// The path should be the entire circle.
let circlePath = NSBezierPath.init()
circlePath.appendArc(withCenter: CGPoint(x: mouseLocation.x, y: mouseLocation.y), radius: (frame.size.width - 10)/2, startAngle: CGFloat(90.0), endAngle: CGFloat(-270.0), clockwise: true) // start from up
// Setup the CAShapeLayer with the path, colors, and line width
circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
circleLayer.fillColor = NSColor.clear.cgColor
circleLayer.strokeColor = color.cgColor
circleLayer.lineWidth = 5.0;
circleLayer.strokeEnd = 1.0
circleLayer.frame = frame
// Add the circleLayer to the view's layer's sublayers
self.view.layer?.addSublayer(circleLayer)
self.undoManager?.setActionName("Draw Bezier Path")
}
答案 0 :(得分:1)
谢谢Willeke。
我添加了使用附加的两个函数将bezierPath添加到新图层的撤消操作。并且稍微更改了上面的代码。
将“self.view.layer?.addSublayer(circleLayer)”替换为 “self.addSublayer(layer:circleLayer)”
删除“self.undoManager?.setActionName(”绘制Bezier路径“)”
在
下添加两个功能func addSublayer(layer: CALayer) {
undoManager?.registerUndo(withTarget: self, selector: #selector(removeSublayer), object: layer)
self.undoManager?.setActionName("Draw Bezier Path")
self.view.layer?.addSublayer(layer)
}
func removeSublayer(layer: CALayer) {
undoManager?.registerUndo(withTarget: self, selector: #selector(addSublayer), object: layer)
layer.removeFromSuperlayer()
}