有一個NSImageView的子類,CALayer的實例被創建,所以我們在圖像上看到一個矩形。問題是如何在鼠標關閉時(當鼠標指針位於矩形內時)移動此矩形並拖動。當鼠標向上時,這個矩形(CALayer)應該保持在圖像上的新位置。如何在NSImageView上移動CALayer
例如
class ImageViewWithRectangle: NSImageView
{
var shape : CAShapeLayer!
func drawRectangle()
{
shape = CAShapeLayer()
shape.lineWidth = 1.0
shape.fillColor = NSColor.clear().cgColor
shape.strokeColor = NSColor.gray().cgColor
shape.lineDashPattern = [1,1]
self.layer?.addSublayer(shape)
let path = CGMutablePath()
path.moveTo(nil, x: 1, y: 1)
path.addLineTo(nil, x: 1, y: 50)
path.addLineTo(nil, x: 50, y: 50)
path.addLineTo(nil, x: 50, y: 1)
path.closeSubpath()
self.shape.path = path
}
}
我會建議顯示你到目前爲止所做的代碼。 –
@Jeshua Lacock我添加了一個示例 – VYT
鼠標事件在哪裏? https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html –