Swift 我的代码没有画一条线,它只画了一个很小的圆

Swift 我的代码没有画一条线,它只画了一个很小的圆,swift,drawing,Swift,Drawing,我的代码没有按照我想要的方式运行,为什么会这样?我不明白出了什么问题。所以我在玩swift,试图从android过渡到swift,我在尝试制作这个简单的应用程序,它没有按照我想要的那样工作,有人能帮我解决我做错了什么吗 // // DrawView.swift // IOSTouch import Foundation import UIKit class DrawView: UIView { var currentLine: Line? var finishedLin

我的代码没有按照我想要的方式运行,为什么会这样?我不明白出了什么问题。所以我在玩swift,试图从android过渡到swift,我在尝试制作这个简单的应用程序,它没有按照我想要的那样工作,有人能帮我解决我做错了什么吗

 //
//  DrawView.swift
//  IOSTouch

import Foundation
import UIKit

class DrawView: UIView {
    var currentLine: Line?
    var finishedLines = [Line]();
    //for debug
    let line1 = Line(begin: CGPoint(x:50,y:50), end: CGPoint(x:100,y:100));
    let line2 = Line(begin: CGPoint(x:50,y:100), end: CGPoint(x:100,y:300));



    func strokeLine(line: Line){
        //Use BezierPath to draw lines
        let path = UIBezierPath();
        path.lineWidth = 5;
        path.lineCapStyle = CGLineCap.round;

        path.move(to: line.begin);
        path.addLine(to: line.end);
        path.stroke(); //actually draw the path
    }

    override func draw(_ rect: CGRect) {

        //draw the finished lines
        UIColor.black.setStroke() //finished lines in black
        for line in finishedLines{
            strokeLine(line: line);
        }

        //for debug
        strokeLine(line: line1);
        strokeLine(line: line2);

        //draw current line if it exists
        if let line = currentLine {
            UIColor.red.setStroke(); //current line in red
            strokeLine(line: line);
        }
    }
    //Override Touch Functions
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print(#function) //for debugging
        let touch = touches.first!; //get first touch event and unwrap optional
        let location = touch.location(in: self); //get location in view co-ordinate
        currentLine = Line(begin: location, end: location);
        setNeedsDisplay(); //this view needs to be updated
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        //TODO

        setNeedsDisplay()
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        //TODO

        setNeedsDisplay()
    }

    override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
        //TODO

    }
    @IBInspectable var finishedLineColor: UIColor = UIColor.black {
        didSet {
            setNeedsDisplay()
        }
    }
    @IBInspectable var currentLineColor: UIColor = UIColor.red {
        didSet {
            setNeedsDisplay()
        }
    }
    @IBInspectable var lineThickness: CGFloat = 10 {
        didSet {
            setNeedsDisplay()
        }
    }
}

此代码有效,我使用它:

import UIKit

class ViewController: UIViewController {

    var firstPoint: CGPoint?
    var secondPoint: CGPoint?
    var currentLine: CAShapeLayer?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.move(to: start)
        linePath.addLine(to: end)
        line.path = linePath.cgPath
        line.strokeColor = UIColor.black.cgColor
        line.lineWidth = 3
        line.lineJoin = kCALineJoinRound
        self.view.layer.addSublayer(line)
    }

    func setCurrentLine(fromPoint start: CGPoint, toPoint end: CGPoint) {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.move(to: start)
        linePath.addLine(to: end)
        line.path = linePath.cgPath
        line.strokeColor = UIColor.red.cgColor
        line.lineWidth = 3
        line.lineJoin = kCALineJoinRound
        currentLine = line
        if let current = currentLine {
            self.view.layer.addSublayer(current)
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        firstPoint = touches.first?.location(in: self.view)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        currentLine?.removeFromSuperlayer()
        currentLine = nil
        if let first = firstPoint, let current = touches.first?.location(in: self.view) {
            setCurrentLine(fromPoint: first, toPoint: current)
        }
    }



    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        secondPoint = touches.first?.location(in: self.view)
        if let first = firstPoint, let second = secondPoint {
            addLine(fromPoint: first, toPoint: second)
            currentLine?.removeFromSuperlayer()
            currentLine = nil
        }
    }
}
现在应该可以了

覆盖func touchesMoved_uuTOUCHES:Set,事件:UIEvent?{

    print(#function) 
    let touch = touches.first!
    let location = touch.location(in: self);
    currentLine?.end = location;
    setNeedsDisplay();
    print(#function) //for debugging
    if var line = currentLine {
        let touch = touches.first!;
        let location = touch.location(in: self);
        line.end = location;
        finishedLines.append(line);
    }
    currentLine = nil;
    setNeedsDisplay();
}

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
    print(#function) //for debugging
    currentLine = nil;
    setNeedsDisplay();
}
}

覆盖func touchesEnded\uuTOUCHEND:Set,事件:UIEvent?{

    print(#function) 
    let touch = touches.first!
    let location = touch.location(in: self);
    currentLine?.end = location;
    setNeedsDisplay();
    print(#function) //for debugging
    if var line = currentLine {
        let touch = touches.first!;
        let location = touch.location(in: self);
        line.end = location;
        finishedLines.append(line);
    }
    currentLine = nil;
    setNeedsDisplay();
}

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
    print(#function) //for debugging
    currentLine = nil;
    setNeedsDisplay();
}