此演示使用 NSPoints 的数据数组来创建一个交互式图形,该图形允许使用鼠标单独移动数据点。它不解决多点选择问题,需要按比例放大以满足您的需求(我没有用超过 20 个点对其进行测试;希望它不会陷入困境)。该演示可以从终端或 Xcode 中的命令行运行,方法是添加一个“main.swift”文件并将现有的 AppDelegate 替换为下面的相应类。
// May be run in Terminal using:
// swiftc ptinrect.swift -framework Cocoa -o ptinrect && ./ptinrect
// or in Xcode with instructions above.
import Cocoa
let path = NSBezierPath()
var R = [NSRect]()
var selected = [Bool]()
var data = [NSPoint]()
class CustomView: NSView {
override func draw(_ rect: NSRect ) {
let bkgrnd = NSBezierPath(rect: rect)
NSColor.lightGray.set()
bkgrnd.fill()
// circles
NSColor.black.set()
for x in stride(from:0, to:R.count, by:1) {
let circle = NSBezierPath(ovalIn:R[x])
circle.fill()
}
// lines
NSColor.white.set()
path.lineWidth = 2.0
path.move(to:data[0])
for x in stride(from:0, to:data.count, by:1) {
path.line(to:data[x])
}
path.stroke()
}
override func mouseDown(with event: NSEvent) {
let wndPt: NSPoint = event.locationInWindow
let pt:NSPoint = self.convert(wndPt, from: nil)
print(pt)
print(R.count)
for x in stride(from:0, to:R.count, by:1) {
if NSPointInRect(pt,R[x]){
selected[x] = true
print("mouse down in rect: \(x)")
print([selected])
} else {selected[x] = false}
}
}
override func mouseUp(with event: NSEvent) {
let wndPt: NSPoint = event.locationInWindow
let pt:NSPoint = self.convert(wndPt, from: nil)
print(pt)
for x in stride(from:0, to:R.count, by:1) {
if NSPointInRect(pt,R[x]){
print("mouse up in rect: \(x)")
}
}
path.removeAllPoints()
self.needsDisplay = true
}
override func mouseDragged(with event: NSEvent) {
let wndPt: NSPoint = event.locationInWindow
let pt:NSPoint = self.convert(wndPt, from: nil)
for x in stride(from:0, to:R.count, by:1) {
if (selected[x] == true){
print("mouse dragged in rect: \(x)")
data[x] = pt
R[x].origin.x = pt.x - 5
R[x].origin.y = pt.y - 5
}
}
path.removeAllPoints()
self.needsDisplay = true
}
}
class ApplicationDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
func buildPath() {
var count: CGFloat = 0
for x in stride(from:0, to:20, by:1) {
let offset = CGFloat(count * 20.0)
data.append(NSPoint.init())
data[x] = NSMakePoint( 20 + offset, 20 + offset)
R.append(NSRect.init())
// x,y coordinates -5 to center rect on data pt
R[x] = NSMakeRect(data[x].x - 5,data[x].y - 5,10,10)
selected.append(Bool.init())
selected[x] = false
count += 1
}
print([data])
path.move(to:data[0])
for x in stride(from:0, to:data.count, by:1) {
path.line(to:data[x])
}
}
func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}
func buildWnd() {
let _wndW : CGFloat = 800
let _wndH : CGFloat = 600
window = NSWindow(contentRect: NSMakeRect( 0, 0, _wndW, _wndH ), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
window.center()
window.title = "Swift Test Window"
window.makeKeyAndOrderFront(window)
// **** Custom view **** //
let view = CustomView( frame:NSMakeRect(20, 60, _wndW - 40, _wndH - 80))
view.autoresizingMask = [.maxXMargin,.minYMargin, .height, .width]
window.contentView!.addSubview (view)
// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
quitBtn.bezelStyle = .circular
quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
quitBtn.title = "Q"
quitBtn.action = #selector(NSApplication.terminate)
window.contentView!.addSubview(quitBtn)
}
func applicationDidFinishLaunching(_ notification: Notification) {
buildMenu()
buildWnd()
buildPath()
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
let appDelegate = ApplicationDelegate()
// **** main.swift **** //
let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = appDelegate
application.activate(ignoringOtherApps:true)
application.run()