我在
Swift中写一个函数来检测我点击的六边形.但是我遇到了一个奇怪的错误消息,指出我不能添加两个CGFloats.无论我做了什么,例如改变为var,单独声明和分配,没有工作.我想有一些其他的错误,但我找不到.代码如下:
func pointToCoordinate(position: CGPoint) -> (Int,Int) { var gridHeight = 2 * singleRadius / sqrt(CGFloat(3)) * 1.5 var gridWidth = 2 * singleRadius var halfWidth = singleRadius var relativePosition = CGPoint(x: position.x - totalRadius / 2,y: position.y - totalRadius / 2) println((relativePosition.y / gridHeight)) var row = -(cgfloatToInt)(relativePosition.y / gridHeight) var column: Int println((relativePosition.x + halfWidth * CGFloat(row + 1)) / (gridWidth)) column = cgfloatToInt((relativePosition.x + halfWidth * CGFloat(row + 1)) / (gridWidth)) // var innerY: CGFloat = CGFloat(relativePosition.y) + CGFloat(gridHeight * row) var innerX = relativePosition.x var innerY = relativePosition.y innerY = innerY - CGFloat(gridHeight * row) println((innerX,innerY)) return (column,row) }
错误信息是错误的.问题是你正在尝试增加一个Int和一个CGFloat.
更换:
innerY = innerY - CGFloat(gridHeight * row)
有:
innerY = innerY - gridHeight * CGFloat(row)
以上答案是您当前版本的代码.对于与您发布的错误消息相对应的已注释版本:
更换:
var innerY: CGFloat = CGFloat(relativePosition.y) + CGFloat(gridHeight * row)
同
var innerY: CGFloat = CGFloat(relativePosition.y) + gridHeight * CGFloat(row)