2017-09-07 124 views
0

在我的Mac OS應用程序中,我需要一個功能,以便在NSImageView中發生mouseDown事件(由用戶觸發)後能夠在NSView內移動NSImageView。當用戶觸發鼠標上移事件時,此視圖必須移至最後一個mouseDrag事件方向並停留在那裏。在移動過程中,我希望NSImageView在屏幕上可見(它應該隨鼠標光標一起移動)。如何將NSImageView移動到Cocoa中的NSView中?

我讀過由蘋果處理鼠標事件的引導,https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html 還有我已經下載這個示例代碼:https://developer.apple.com/library/content/samplecode/DragItemAround/Introduction/Intro.html

兩個鏈接包含代碼的目的C.規範DragItemAround是過時的。我試圖在GitHub和其他StackOverflow線程上搜索解決方案,但還沒有找到任何工作解決方案。

很高興聽到這個問題的答案。我使用的是斯威夫特3.

我創建一個自定義MovableImageView是NSImageView與此代碼的子類:

import Cocoa 

class MovableImageView: NSImageView { 

    override func draw(_ dirtyRect: NSRect) { 
     super.draw(dirtyRect) 

     // Drawing code here. 
     // setup the starting location of the 
     // draggable item 

    } 

    override func mouseDown(with event: NSEvent) { 
     Swift.print("mouseDown") 

     //let window = self.window! 
     //let startingPoint = event.locationInWindow 


    } 


    override func mouseDragged(with event: NSEvent) { 
     Swift.print("mouseDragged") 

     self.backgroundColor = NSColor.black 

    } 

    override func mouseUp(with event: NSEvent) { 
     Swift.print("mouseUp") 
    } 

} 

之後在Interface Builder中我已經設置該類NSImageView。我也爲這個NSImageView在Interface Builder中設置了約束。

現在我想弄清楚如何在NSView中移動NSImageView? (Swift 3,XCode 8)

+0

你繼承了錯誤的生物。代替NSView的子類。 –

+2

@ElTomato不,NSImageVIew是NSControl的子類,它是NSView的子類。所以這應該沒問題。 – brianLikeApple

回答

1

您需要保存鼠標點並稍後使用它作爲偏移量。請檢查下面的代碼:

class MovableImageView: NSImageView { 

var firstMouseDownPoint: NSPoint = NSZeroPoint 

init() { 
    super.init(frame: NSZeroRect) 
    self.wantsLayer = true 
    self.layer?.backgroundColor = NSColor.red.cgColor 
} 

required init?(coder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override func draw(_ dirtyRect: NSRect) { 
    super.draw(dirtyRect) 

    // Drawing code here. 
} 

override func mouseDown(with event: NSEvent) { 
    Swift.print("mouseDown") 
    firstMouseDownPoint = (self.window?.contentView?.convert(event.locationInWindow, to: self))! 
} 

override func mouseDragged(with event: NSEvent) { 
    Swift.print("mouseDragged") 
    let newPoint = (self.window?.contentView?.convert(event.locationInWindow, to: self))! 
    let offset = NSPoint(x: newPoint.x - firstMouseDownPoint.x, y: newPoint.y - firstMouseDownPoint.y) 
    let origin = self.frame.origin 
    let size = self.frame.size 
    self.frame = NSRect(x: origin.x + offset.x, y: origin.y + offset.y, width: size.width, height: size.height) 
} 

override func mouseUp(with event: NSEvent) { 
    Swift.print("mouseUp") 

    } 
} 

在父視圖,只需添加這MovableImageView作爲子視圖這樣的:

let view = MovableImageView() 
view.frame = NSRect(x: 0, y: 0, width: 100, height: 100) 
self.view.addSubview(view) //Self is your parent view 

enter image description here

+0

這很愚蠢。 Adelmaer說他或她有一個帶有一個或多個對象的NSView容器,可以用鼠標指針重新定位。在這種情況下,您應該繼承NSView。 –

+2

@ElTomato NSImageView最終是從NSView繼承而來,並把NSImageView放在NSView裏面是很常見的OOP。它有什麼問題? – brianLikeApple

+0

感謝@brianLikeApple,您的解決方案非常棒! – Adelmaer