有一个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 –