简体   繁体   中英

How to draw a line from one point to the next point in Swift

I would like to know how to draw a line from one point to another where the user touch the first point and the next point he touches, it create a line in between. I know that using UIBezierPath will be useful in here but I am still new to Swift and iOS platform so any guidance will helpful.

在此处输入图片说明 I have created a function called drawLineFromPoint but does not work for me.

import UIKit
import GLKit
class ViewController: GLKViewController {
private var context: EAGLContext?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    EAGLcontext()
    // Gesture Code
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first{

        let position = touch.location(in: view)
        var dot = UIView(frame: CGRect(x: position.x, y: position.y, width: 10, height: 10))
        dot.backgroundColor = .red
        view.addSubview(dot)
        // View the x and y coordinates
        print(position)

    }
}

//Not sure how to do this part ???
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {

    let path = UIBezierPath()
    path.move(to: start)
    path.addLine(to: end)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = UIColor.green.cgColor
    shapeLayer.lineWidth = 1.0

    view.layer.addSublayer(shapeLayer)
}

//Create EAGL Context for the GLKView
private func EAGLcontext() {

    context = EAGLContext(api: .openGLES2)

    EAGLContext.setCurrent(context)

    if let view = self.view as? GLKView, let context = context {

        view.context = context
        delegate = self
    }
}

override func glkView(_ view: GLKView, drawIn rect: CGRect) {
    //Set the color
    glClearColor(0.85, 0.85, 0.85, 1.0)

    glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
    }
}


extension ViewController: GLKViewControllerDelegate {
func glkViewControllerUpdate(_ controller: GLKViewController) {     
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }   
}

Based On your code I changed some things and this code Works please check.

    class ViewController: UIViewController {


        var lastPosition: CGPoint?
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            // Gesture Code
        }

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first{

                let position = touch.location(in: view)
            if let lastPosition = self.lastPosition {
                self.drawLineFromPoint(start: lastPosition, toPoint: position, ofColor: UIColor.red, inView: self.view)
            }
                self.lastPosition = position
                // View the x and y coordinates
                let dot = UIView(frame: CGRect(x: position.x, y: position.y, width: 10, height: 10))
                dot.backgroundColor = .red
                view.addSubview(dot)
                print(position)

            }
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first, let lastPosition = self.lastPosition{

                let position = touch.location(in: view)
                self.drawLineFromPoint(start: lastPosition, toPoint: position, ofColor: UIColor.red, inView: self.view)
                self.lastPosition = position
                let dot = UIView(frame: CGRect(x: position.x, y: position.y, width: 10, height: 10))
                dot.backgroundColor = .red
                view.addSubview(dot)
            }
        }

        //Not sure how to do this part ???
        func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {

            let path = UIBezierPath()
            path.move(to: start)
            path.addLine(to: end)

            let shapeLayer = CAShapeLayer()
            shapeLayer.path = path.cgPath
            shapeLayer.strokeColor = UIColor.green.cgColor
            shapeLayer.lineWidth = 1.0

            view.layer.addSublayer(shapeLayer)
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM