【发布时间】:2023-03-31 20:02:02
【问题描述】:
我正在尝试制作一个简单的绘图应用程序(OSX Mac 应用程序),并且我试图弄清楚用户如何通过两次鼠标单击来绘制一条线,例如,第一次鼠标单击(mouseDown 然后 mouseUP)将标记线的起点,第二次鼠标单击(mouseDown 然后 mouseUP)将标记线的终点。在用户第二次单击终点之前,我希望线(在锚定终点之前)实时显示,有点像在 Photoshop 中。 Objective-C 和 Swift 都很好。
到目前为止,我已经...
var newLinear = NSBezierPath()
override func mouseDown(theEvent: NSEvent) {
super.mouseDown(theEvent)
var lastPoint = theEvent.locationInWindow
lastPoint.x -= frame.origin.x
lastPoint.y -= frame.origin.y
newLinear.moveToPoint(lastPoint)
}
override func mouseUp(theEvent: NSEvent) {
var newPoint = theEvent.locationInWindow
newPoint.x -= frame.origin.x
newPoint.y -= frame.origin.y
newLinear.lineToPoint(newPoint)
needsDisplay = true
}
干杯!
【问题讨论】:
-
使用二进制状态机
var gotFirstPoint = false,如果false在mouseClick上设置它true并保存原点,如果true在mouseMove中检查值并重绘,如果@则在mouseClick中完成线987654326@? -
到目前为止,我可以通过拖动来绘制线条,但即使在拖动时,我也看不到线条。我想实时显示我拖动的线条。
-
在 mouseMove 例程中,您需要捕获终点并设置视图的
needsDisplay,然后在drawRect中检查您当前是否正在拖动,如果是则绘制线。应该工作得很好...... -
查看示例应用 Sketch。
-
如果我有源代码,那就太好了。
标签: objective-c swift macos nsview