【发布时间】:2017-01-11 17:01:08
【问题描述】:
NSImageView 有一个子类,并创建了 CALayer 的实例,所以我们在图像上看到了一个矩形。问题是如何在鼠标按下(当鼠标指针在矩形内)并拖动时移动这个矩形。当鼠标向上时,这个矩形(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 我添加了一个示例
-
@Jeshua Lacock 这是我的问题,我应该如何使用鼠标事件来实现这样的效果。
-
阅读上面提到的文档?
标签: swift calayer nsimageview