【问题标题】:my code doesn't draw a line, it only draws a very tiny circle我的代码没有画线,它只画了一个很小的圆圈
【发布时间】:2018-03-27 02:20:45
【问题描述】:

我的代码没有按照我想要的方式运行,为什么会这样?我不明白出了什么问题。所以我正在玩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 Foundation
import CoreGraphics

struct Line {
    var begin = CGPoint.zero
    var end = CGPoint.zero
}

【问题讨论】:

    标签: swift drawing


    【解决方案1】:

    此代码有效,我使用它:

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

    【讨论】:

      【解决方案2】:

      它现在应该可以工作了

      覆盖 func touchesMoved(_ touches: Set, with event: UIEvent?) {

          print(#function) 
          let touch = touches.first!
          let location = touch.location(in: self);
          currentLine?.end = location;
          setNeedsDisplay();
      

      }

      override func touchesEnded(_ touches: Set, with event: UIEvent?) {

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-02
        • 2012-11-12
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多