0

I'm working on an app that makes points and lines, the problem happens when i open a new view because the main view keeps drawing the touches of the new view.

Here is my code. Most of the code is just to know in which quadrant the point is drawn to create a UILabel, also to know if a point is gonna be drawn near another and those things.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        print(i)
        let location = touch.location(in: imageView)
        xpoints.append(location.x)
        ypoints.append(location.y)
        print(xpoints[i], ", ", ypoints[i])
        if i == 0{
            lastPoint = location
            p = true
            drawLine(from: lastPoint, to: lastPoint)
            xpoint = Int(xpoints[i])
            ypoint = Int(ypoints[i])
            if xpoint <= 207 && ypoint <= 448{
                let labelView = UILabel(frame: CGRect(x: xpoints[i]-40, y: ypoints[i]-40, width: 50, height: 25))
                labelView.numberOfLines = 2
                labelView.font = labelView.font.withSize(10)
                labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                self.view.addSubview(labelView)
            } else if xpoint > 207 && ypoint <= 448{
                let labelView = UILabel(frame: CGRect(x: xpoints[i]+10, y: ypoints[i]-40, width: 50, height: 25))
                labelView.numberOfLines = 2
                labelView.font = labelView.font.withSize(10)
                labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                self.view.addSubview(labelView)
            } else if xpoint <= 207 && ypoint > 448{
                let labelView = UILabel(frame: CGRect(x: xpoints[i]-40, y: ypoints[i]+10, width: 50, height: 25))
                labelView.numberOfLines = 2
                labelView.font = labelView.font.withSize(10)
                labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                self.view.addSubview(labelView)
            } else if xpoint > 207 && ypoint > 448{
                let labelView = UILabel(frame: CGRect(x: xpoints[i]+10, y: ypoints[i]+10, width: 50, height: 25))
                labelView.numberOfLines = 2
                labelView.font = labelView.font.withSize(10)
                labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                self.view.addSubview(labelView)
            }
        } else{
            for j in 0..<i {
                if xpoints[i] >= (xpoints[j]-20) && xpoints[i] <= (xpoints[j]+20) && ypoints[i] >= (ypoints[j]-20) && ypoints[i] <= (ypoints[j]+20){
                    xpoints[i] = xpoints[j]
                    ypoints[i] = ypoints[j]
                    repeated = true
                }
            }
            if repeated{
            newlocation.x = xpoints[i]
            newlocation.y = ypoints[i]
            currentPoint = newlocation
            p = true
            drawLine(from: currentPoint, to: currentPoint)
            l = true
            drawLine(from: lastPoint, to: currentPoint)
            lastPoint = currentPoint
            repeated = false
            } else{
                currentPoint = location
                p = true
                drawLine(from: currentPoint, to: currentPoint)
                xpoint = Int(xpoints[i])
                ypoint = Int(ypoints[i])
                if xpoint <= 207 && ypoint <= 448{
                    let labelView = UILabel(frame: CGRect(x: xpoints[i]-40, y: ypoints[i]-40, width: 50, height: 25))
                    labelView.numberOfLines = 2
                    labelView.font = labelView.font.withSize(10)
                    labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                    self.view.addSubview(labelView)
                } else if xpoint > 207 && ypoint <= 448{
                    let labelView = UILabel(frame: CGRect(x: xpoints[i]+10, y: ypoints[i]-40, width: 50, height: 25))
                    labelView.numberOfLines = 2
                    labelView.font = labelView.font.withSize(10)
                    labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                    self.view.addSubview(labelView)
                } else if xpoint <= 207 && ypoint > 448{
                    let labelView = UILabel(frame: CGRect(x: xpoints[i]-40, y: ypoints[i]+10, width: 50, height: 25))
                    labelView.numberOfLines = 2
                    labelView.font = labelView.font.withSize(10)
                    labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                    self.view.addSubview(labelView)
                } else if xpoint > 207 && ypoint > 448{
                    let labelView = UILabel(frame: CGRect(x: xpoints[i]+10, y: ypoints[i]+10, width: 50, height: 25))
                    labelView.numberOfLines = 2
                    labelView.font = labelView.font.withSize(10)
                    labelView.text = "X: \(xpoint)\nY: \(ypoint)"
                    self.view.addSubview(labelView)
                }
                l = true
                drawLine(from: lastPoint, to: currentPoint)
                lastPoint = currentPoint
            }
        }
        p = false
        l = false
        i += 1
    }
}

func drawLine(from lastPoint: CGPoint, to toPoint: CGPoint){
    UIGraphicsBeginImageContext(view.frame.size)
    guard let context = UIGraphicsGetCurrentContext()
        else{
            return
    }
    imageView.image?.draw(in: view.bounds)
    context.move(to: lastPoint)
    context.addLine(to: toPoint)
    context.setLineCap(.round)
    context.setBlendMode(.normal)
    if p {
        context.setLineWidth(20)
        context.setStrokeColor(UIColor.black.cgColor)
    }
    if l {
        context.setLineWidth(3)
        context.setStrokeColor(UIColor.systemBlue.cgColor)
    }
    context.strokePath()
    imageView.image = UIGraphicsGetImageFromCurrentImageContext()
    imageView.alpha = 1
    UIGraphicsEndImageContext()
}
carlosobedgomez
  • 161
  • 1
  • 2
  • 11
  • 1
    Unrelated to your question, but you really shouldn't be using hardcoded numbers to check coordinates of touches. How will that translate to different screen sizes correctly? [Methods](https://stackoverflow.com/questions/2793242/detect-if-certain-uiview-was-touched-amongst-other-uiviews) already exist for checking if a touch happened inside a `UIView`, leverage those. – Dávid Pásztor Oct 28 '19 at 00:29
  • Really helpful! I tried a lot with other methods but i hadn't seen those, there i found the perfect one(8 thanks! – carlosobedgomez Oct 28 '19 at 16:57

0 Answers0