gpt4 book ai didi

swift - NSImageView mouseDragged 值传递回 windowController

转载 作者:行者123 更新时间:2023-11-30 13:38:33 26 4
gpt4 key购买 nike

谁能告诉我如何将位置 X 和 Y 返回到我的 WindowController NSTextField(testTextX 和 testTextY)?

我以编程方式创建了一个 windowController、NSImage 和 NSTextField。我向 NSView 添加了一个图像“点”,并且希望“点”图像可拖动并返回位置 X 和 Y。

我设法添加了图像并可拖动,但我不知道如何将 mouseDragged 上的位置 X 和 Y 传递回我的 WindowController 处的 NSTextField(testTextX 和 testTextY)?

当前显示如下,点击“点”图片可拖动到任意位置。

当图像被拖动时,我可以从 NSImageView func mouseDragged(theEvent: NSEvent) 打印出位置 X 和 Y。

我陷入了如何将位置 X 和 Y 传递回 WindowController 的困境。

enter image description here我的WindowController.swift代码如下:

public class MyWindowController: NSWindowController, NSWindowDelegate {
// MARK: Constants
// 1. Define min&max window size
var fWidth:CGFloat = 0
var fHeight:CGFloat = 0
var vWidth:CGFloat = 0
var vHeight:CGFloat = 0
var blankPage: NSView!

var viewDotImg: NSImageView!
var testTextX: NSTextField!
var testTextY: NSTextField!

var modalWinHeight: CGFloat = 0
var m_window:NSWindow = NSWindow()

// MARK: Initializer
init(){
super.init(window: self.m_window)
}

init(channelId:Int){
super.init(window:self.m_window)

// Get the window full width and height
fWidth = (NSScreen.mainScreen()?.frame.width)!
fHeight = (NSScreen.mainScreen()?.frame.height)!

// Get the window visible width and height
vWidth = (NSScreen.mainScreen()?.visibleFrame.width)!
vHeight = (NSScreen.mainScreen()?.visibleFrame.height)!

// Get the position X and Y for Window
let winPosX: CGFloat = fWidth - vWidth
let winPosY: CGFloat = fHeight - vHeight

// Divide the height by 3
let avgHeight: CGFloat = vHeight / 3

// Set the max window height as avgHeight * 1.3
let modalWinHeight: CGFloat = avgHeight * 1.3

// Set the window frame
self.m_window = NSWindow(contentRect: NSMakeRect(winPosX, winPosY, vWidth, modalWinHeight),
styleMask: NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask,
backing: NSBackingStoreType.Buffered, `defer`: false);

// Window delegate
self.m_window.delegate = self

// Add a blank NSView with Gradian background colour
blankPage = CGradiantBlank(x: x, y:y, width:width, height: height)
self.window?.contentView = blankPage

// Add Image Dot to NSView
viewDotImg = DragNSImageView(x: 200, y: 200, width: 10, height: 10)

// Add Two TextField to NSView
testTextX = TextLabel(x: 200, y: 10, width: 100, height: 24, value: "100")
testTextY = TextLabel(x: 305, y: 10, width: 100, height: 24, value: "100")

blankPage.addSubview(viewDotImg)
blankPage.addSubview(testTextX)
blankPage.addSubview(testTextY)
}

func updTestXY(x:Double, y:Double){
testTextX.stringValue = String(x)
testTextY.stringValue = String(y)
}

override public init(window: NSWindow?) {
super.init(window: window)
}

required public init?(coder:NSCoder){
super.init(coder: coder)
}
}

我的NSImageView代码如下:

public class DragNSImageView: NSImageView {
private var xWidth: CGFloat = 0
private var xHeight: CGFloat = 0

init(x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat, chId: Int) {
super.init(frame: NSRect(x: x,y: y,width: width,height: height))
super.imageScaling = NSImageScaling.ScaleAxesIndependently
super.image = NSImage(named: "dot-10")
xWidth = width
xHeight = height
}

required public init?(coder: NSCoder) {
super.init(coder: coder)
}

override public func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}

public override func mouseDragged(theEvent: NSEvent) {
super.mouseDown(theEvent)
let location = theEvent.locationInWindow
let pX: CGFloat = location.x
let pY: CGFloat = location.y
super.frame = NSRect(x: pX,y: pY,width: xWidth,height: xHeight)
Swift.print("X: \(location.x)" Y: \(location.y))
/* How to pass this location.x and location.y back to WindowController func updateTestXY()?

updTestXY(Double(location.x), y:Double(location.y))
*/
}
}

我的 NSTextField 代码如下:

public class TextLabel: NSTextField {
// MARK: Initializer
init(){
super.init(frame: NSRect(x: 0, y: 0, width: 90, height: 10))
}

convenience init(x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat, value:String){
self.init()
super.wantsLayer = true
super.backgroundColor = NSColor.darkGrayColor()
super.textColor = NSColor.whiteColor()
super.stringValue = value
super.frame = NSRect(x: x, y: y, width: width, height: height)
}

required public init?(coder: NSCoder) {
super.init(coder: coder)
}

override public func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}
}

最佳答案

好的,我找到了解决方案。

let mainWindow = self.window?.windowController as! MyWindowController
mainWindow.updXY(location.x, y:location.y, refName: refName)

如果有多个“点”图像,则将以下代码添加到 NSImageView init() 中

super.identifier = "ReferenceName1"

现在,转到 NSImageView mouseDragged(theEvent: NSEvent) 方法并添加以下行:

let refName = super.identifier

这将在 MyWindowController 中使用来检测调用指定函数时拖动了哪个“点”图像。就我而言,我称之为

updXY(location.x, y:location.y, refName: refName)

因此,在 MyWindowController 中,我使用以下代码来完成工作。

public func updXy(x: CGFloat, y: CGFloat, refName: String){
if refName == "ReferenceName1" {

}else if refName == "ReferenceName2" {

}
}

关于swift - NSImageView mouseDragged 值传递回 windowController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35831218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com