I am trying to find a good way to make the mouse movement smoother then just "warping" around. I used to use this code in C# and so I thought I could convert it into Swift. This is what I've got so far but the app crashes as soon as I click the button. What's going wrong?
@IBAction func btnMove(_ sender: Any) {
var test: NSPoint = NSMakePoint(200, 150)
LinearMovement(newPosition: test)
}
func LinearMovement(newPosition: NSPoint) {
let n = Int(readLine()!)!
var start: NSPoint! = NSMakePoint(self.mouseLocation.x, self.mouseLocation.y)
var iterPoint: NSPoint! = start
var slope: NSPoint! = NSPoint(x: newPosition.x - start.x, y: newPosition.y - start.y)
// Convert CGFloat to Int
var myIntValuex:Int = Int(slope.x)
var myIntValuey:Int = Int(slope.y)
// Devide by the number of steps
var slopex = myIntValuex / n
var slopey = myIntValuey / n
// Move the mouse to each iterative point.
for i in 0 ..< n {
var intIterx:Int = Int(iterPoint.x)
var intItery:Int = Int(iterPoint.y)
var iterPointx = intIterx + slopex
var iterPointy = intItery + slopey
var iterPointf: NSPoint = NSPoint(x: iterPointx, y: iterPointy)
CGWarpMouseCursorPosition(iterPointf)
// Thread.Sleep(MouseEventDelayMS) ??????
}
CGWarpMouseCursorPosition(newPosition)
}