繁体   English   中英

如何在Swift中从一个点到下一个点画一条线

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

我想知道如何从一个点到另一个点画一条线,用户触摸第一个点和他触摸的下一个点,它会在两者之间创建一条线。 我知道在这里使用 UIBezierPath 会很有用,但我仍然是 Swift 和 iOS 平台的新手,所以任何指导都会有所帮助。

在此处输入图片说明 我创建了一个名为 drawLineFromPoint 的函数,但对我不起作用。

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.
    }   
}

根据您的代码,我更改了一些内容,此代码有效,请检查。

    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)
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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