Andy Fedoroff - 9 months ago 55

Swift Question

I'm writing a code for app building rectangles. I can separately move rectangles' points with mouse.

How could I move all four points of a rectangle simultaneously?

The principle of operation: if I drag 1 point with a mouse, the other 3 will follow it at the same time with the same offset.

This is how elements were constructed:

`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 with LMB:

`func mouseDragged(event: NSEvent) {`

var point = self.convertPoint(event.locationInWindow, fromView: nil)

updateDragging(point)

}

I need a code (or pseudo code) that allows me to drag all four points of any rectangle at a time.

Answer

Basically, you need to calculate the offset that is being applied to the tracked point and apply that offset to all the other points. It's easiest to do this by separating the calculation and the update, so the update always runs on all points.

To calculate the offset just subtract the current point x and y from the new point x and y.

Something along the lines of:

```
func mouseDragged(event: NSEvent) {
let eventPoint = self.convertPoint(event.locationInWindow, fromView: nil)
let trackingPoint = XXX
let xOffset = eventPoint.x - trackingPoint.x
let yOffset = eventPoint.y - trackingPoint.y
for point in allPoints {
point.x += xOffset
point.y += yOffset
}
}
```

Source (Stackoverflow)