Hunter Hunter - 3 months ago 17
iOS Question

How to convert CGFloat in touchs moved SWIFT XCODE?

Hey in getting the same error a bunch of times because its saying that I need to convert something to Float instead of CGFloat. But the problem is I don't know how to convert it. Also it seems that this is the problem line "let touchPoint = touch.locationInNode(self)". Also the picture linked should explain alot

enter image description here

Code:

//=============================================================================================
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: UITouch in touches {
let touch = touches.first! as UITouch
let touchPoint = touch.locationInNode(self)
if isTracking == true && sqrtf(powf((touchPoint.x - thumbNode.position.x), 2) + powf((touchPoint.y - thumbNode.position.y), 2)) < size * 2 {
if sqrtf(powf((touchPoint.x - self.anchorPointInPoints.x), 2) + powf((touchPoint.y - self.anchorPointInPoints.y), 2)) <= thumbNode.size.width {
var moveDifference = CGPointMake(touchPoint.x - self.anchorPointInPoints().x, touchPoint.y - self.anchorPointInPoints().y)
thumbNode.position = CGPointMake(self.anchorPointInPoints().x + moveDifference.x, self.anchorPointInPoints().y + moveDifference.y)
}
else
{
var vX: Double = touchPoint.x - self.anchorPointInPoints.x
var vY: Double = touchPoint.y - self.anchorPointInPoints.y
var magV: Double = sqrt(vX * vX + vY * vY)
var aX: Double = self.anchorPointInPoints().x + vX / magV * thumbNode.size.width
var aY: Double = self.anchorPointInPoints().y + vY / magV * thumbNode.size.width
thumbNode.position = CGPointMake(aX, aY)
}
}
velocity = CGPointMake((thumbNode.position.x - self.anchorPointInPoints().x), (thumbNode.position.y - self.anchorPointInPoints().y))
angularVelocity = -atan2(thumbNode.position.x - self.anchorPointInPoints.x, thumbNode.position.y - self.anchorPointInPoints.y)


}

}
func anchorPointInPoints() -> CGPoint {
return CGPointMake(25, 15.8)
}

Answer

You can convert it to CGFloat with CGFloat(touchPoint.x), remove all the :Double in the code and just do a type casting when needed like Double(something)

Comments