计算CGPoint从一个地方到另一个地方的距离

Calculating the distance CGPoint travelled from one place to another

我必须使用什么方法来计算 CGPoint 从其旧位置到新位置的行进距离?

var point: CGPoint = CGPointMake(x:50.0, y:50.0)

还有LMB拖动点的方法:

func mouseDragged(event: NSEvent) {

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

使用 Pythagorean theoremmouseLocation = event.mouseLocation(),其中 event 在您的情况下是类型 NSEvent

let point1: CGPoint = CGPoint(x: 2.0, y: 9.0)
let point2: CGPoint = CGPoint(x: 4.0, y: 13.0)

let xDist = (point1.x - point2.x)
let yDist = (point1.y - point2.y)
let distance = sqrt((xDist * xDist) + (yDist * yDist))

print(distance)

point1 将是您的起始位置,您可以从 mouseDragged 函数中的 NSEvent 获取用户点击的位置 - point2

mouseLocation = event.mouseLocation()
point2 = CGPointMake(mouseLocation.x, mouseLocation.y)