I am developing an app, which should draw points when is touch, in the location of touch and when I have got 2 or more points, it should connect these points and fill up the new object.
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)
}
}
}
Everytime i save the x and y coordinates in float type into array pointX and pointsY. But when i need to draw a line between 2 points, it tells me an error Cannot call value of non-function type [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)
Is there any other solution, where can I save my CGPoints?
If I can ask for a code, that will be easier for me, I am not good in english, thank you.
Swift uses square brackets to index arrays, and you are using parentheses.
squarePath.move(to: CGPoint(x: pointsX[0], y: pointsY[0]))