Updated -- I can now drag any NSTextField control around with the following code:
ViewController.swift
class ViewController: NSViewController {
@IBAction override func mouseDragged(theEvent: NSEvent) {
let dragPoint = theEvent.locationInWindow
//var newDragLocation = NSPoint()
print( "dragPoint X,Y = \(dragPoint)" )
if controlToMove != nil {
//newDragLocation = (controlToMove!.convertPoint(dragPoint, fromView: nil))
//print( "newDragLocation X,Y = \(newDragLocation)" )
var cntrl_location = NSRect();
cntrl_location = controlToMove!.frame
print( "cntrl_location X,Y = \(cntrl_location)" )
let previousLocation = cntrl_location
//I think this is the problem with the current dragging? - my location is not the dragPoint
//it is the dragPoint minus/plus some offset to the control frame origin???
var location = CGPoint()
location = dragPoint
var delta_x = CGFloat()
var delta_y = CGFloat()
delta_x = location.x - previousLocation.origin.x
delta_y = location.y - previousLocation.origin.y
print( "delta X,Y = \(delta_x), \(delta_y)" )
// move label
cntrl_location.origin = CGPointMake(location.x + delta_x, location.y + delta_y);
print( "cntrl_location.orgin = \(cntrl_location.origin)" )
// save the new drag location for the next drag event
//lastDragLocation=newDragLocation
controlToMove!.frame = cntrl_location
print( "controlToMove.frame X,Y = \(controlToMove?.frame)" )
lastLocation = cntrl_location.origin
}
}
But it has just a very small problem: when I drag the label or textfield control two of them occur flickering back and fourth as I drag. It has to do with one showing up at the mouse location and one showing up at the new control frame or origin location alternating back and fourth as I drag. I just can't yet figure out the correct logic for the drag yet?
Okay I have a solution finally to the question how do you drag a Label or TextField (NSTextField) control in Mac OS X around a view. I have finalized the solution:
In the ViewController class create a property
class ViewController: NSViewController {
var mouseDownLocation: CGPoint?
}
In the override mouseDown event
var mouseDownEvent: NSEvent?
@IBAction override func mouseDown(theEvent: NSEvent) {
var event_location: NSPoint!
event_location = theEvent.locationInWindow
var cntrl_id = NSTextField()
var cntrl_frame = NSRect()
var cntrl_name = String()
var cntrl_value = String()
var hit = Bool()
for view in self.view.subviews as [NSView] {
if let ct = view as? NSTextField {
cntrl_name = ct.identifier!
cntrl_id = ct
cntrl_frame = ct.frame
cntrl_value = ct.stringValue
hit = cntrl_frame.contains(event_location)
if hit {
controlToMove = cntrl_id
lastLocation = controlToMove?.frame.origin
break
}
}
}
//figure out the difference of where the mouse went down at in the
//control frame in relation to the control frame origin
let dx = event_location.x - (controlToMove?.frame.origin.x)!
let dy = event_location.y - (controlToMove?.frame.origin.y)!
let mouseDownInCntrlFrame = CGPoint(x: dx, y: dy)
mouseDownLocation = mouseDownInCntrlFrame
}
Now in the override mouseDragged event
@IBAction override func mouseDragged(theEvent: NSEvent) {
let dragPoint = theEvent.locationInWindow
if controlToMove != nil {
var cntrl_location = CGPoint();
cntrl_location.x = controlToMove!.frame.origin.x
cntrl_location.y = controlToMove!.frame.origin.y
print( "cntrl_location X,Y = \(cntrl_location)" )
let previousLocation = cntrl_location
var location = CGPoint()
location = dragPoint
location.x -= (mouseDownLocation?.x)!
location.y -= (mouseDownLocation?.y)!
var delta_x = CGFloat()
var delta_y = CGFloat()
delta_x = location.x - previousLocation.x
delta_y = location.y - previousLocation.y
// move label
cntrl_location = CGPointMake(location.x + delta_x, location.y + delta_y);
controlToMove!.frame.origin.x = cntrl_location.x
controlToMove!.frame.origin.y = cntrl_location.y
}
}
This will let you drag any NSTextField around the view that you click down on and drag. It is not quite as smooth as I would like there is just a bit of hoping of the control as you drag it around the view. But considering that there is no other information anywhere on the internet on how to drag an OS X control (there are a couple of blogs and information on how to drag an image - but even those are more of a drag and drop type solution) in swift around a view this is a solution that works. I am sure an experienced OS X Xcode guru could improve on this solution - I welcome a refinement to this solution if some experience OS X developer wishes to contribute.