CGPoint:不能调用非函数类型`[Float]`的值

时间:2018-01-24 09:49:18

标签: swift xcode graphics cgpoint

我正在开发一个应用程序,它应该在触摸时绘制点,在触摸位置,当我有2个或更多点时,它应该连接这些点并填充新对象。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(draw == true){

        for touch in touches {
            let location = touch.location(in: view)

            pointsX.append(Float(location.x))
            pointsY.append(Float(location.y))

            let touchIndicator = UIView(frame: CGRect(x: location.x - 10, y: location.y - 10, width: 20, height: 20))
            touchIndicator.alpha = 0.8
            touchIndicator.backgroundColor = UIColor.red
            touchIndicator.layer.cornerRadius = 10
            self.view.addSubview(touchIndicator)
            touchIndicators.append(touchIndicator)
        }

    }
}

每次我将float类型的x和y坐标保存到数组pointX和pointsY中。但是当我需要在2个点之间画一条线时,它告诉我一个错误不能调用非函数类型[Float]的值。

let squarePath = UIBezierPath()

    squarePath.move(to: CGPoint(x: pointsX(0), y: pointsY(0)))

    squarePath.addLine(to: CGPoint(x: 200, y: 100))
    squarePath.addLine(to: CGPoint(x: 200, y: 200))



    squarePath.close()

    let square = CAShapeLayer()
    square.path = squarePath.cgPath
    square.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(square)

还有其他解决方案,我在哪里可以保存我的CGPoints?

如果我可以要求代码,对我来说会更容易,我的英语不好,谢谢。

2 个答案:

答案 0 :(得分:0)

Swift使用方括号来索引数组,并且您使用括号。

squarePath.move(to: CGPoint(x: pointsX[0], y: pointsY[0]))

答案 1 :(得分:0)

这里我创建了一个允许你将CGPoint转换为双倍

的函数
 func convertCgPointToDoule(location: CGPoint) -> Double {
        let position = sprite.position.y
        return return position - location.y

    }