I'm developing a macOS app that generates rectangles. I can separately move rectangles' points with left mouse button LMB. The principle of operation: when I drag 1 point with a LMB, the other 3 points will follow it at the same time with the same offset.
How can I move all four points of a rectangle simultaneously?
The code for constructing elements:
enum RectPoint {
case point1(point: CGPoint)
case point2(point: CGPoint)
case point3(point: CGPoint)
case point4(point: CGPoint)
func pointCoord() -> [CGPoint] {
switch self {
case .point1(let point): return [point]
case .point2(let point): return [point]
case .point3(let point): return [point]
case .point4(let point): return [point]
}
}
}
class SpecialView: NSView {
var array: [RectPoint] = []
private var trackVertex: RectPoint?
private var trackVertexIndex: Int?
private var trackElementIndex: Int?
private func updateDragging(point: CGPoint) {
guard let trackVertex = self.trackVertex,
let trackVertexIndex = self.trackVertexIndex,
let trackElementIndex = self.trackElementIndex
else { return }
let newVertex = trackVertex.debugReleaseChecking(point,
atElementIndex: trackElementIndex)
array[trackVertexIndex] = newVertex
self.needsDisplay = true
}
}
Here's a method for dragging points:
func mouseDragged(event: NSEvent) {
var point = self.convertPoint(event.locationInWindow, fromView: nil)
updateDragging(point)
}