在swift 3和核心图形中绘制一条直线

maz*_*zen 3 drawing core-graphics sprite-kit swift

我正在尝试使用核心图形和swift 3绘制单个直线.然而,当调用touchesmoved时,它会创建多条线而不是一条线.使用的代码如下:

import UIKit

    class ViewController: UIViewController {

        @IBOutlet weak var drawingPlace: UIImageView!

        var startTouch : CGPoint?
        var secondTouch : CGPoint?

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let touch = touches.first
            startTouch = touch?.location(in: drawingPlace)
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in touches{
                secondTouch = touch.location(in: drawingPlace)

                UIGraphicsBeginImageContext(drawingPlace.frame.size)
                drawingPlace.image?.draw(in: CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height))
                let bezier = UIBezierPath()

                bezier.move(to: startTouch!)
                bezier.addLine(to: secondTouch!)
                bezier.close()

                bezier.lineWidth = 4
                UIColor.blue.set()
                bezier.stroke()

                let img = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                drawingPlace.image = img   
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

btw touchesEnd创建一条线,但仅在用户结束触摸后.我希望用户在触摸时看到正在绘制的线条.谢谢.

Rei*_*ian 5

您需要保持当前context 直到线条绘制过程结束,之后您需要保留当前图像并清除context,再次绘制已保存的图像,并绘制新线条.当触摸结束时调用清除context当前图像并保存当前图像,之后再次开始新的线条绘制过程

这是完整的代码

import UIKit

class ViewController2: UIViewController {

    @IBOutlet weak var drawingPlace: UIImageView!

    var startTouch : CGPoint?
    var secondTouch : CGPoint?
    var currentContext : CGContext?
    var prevImage : UIImage?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        startTouch = touch?.location(in: drawingPlace)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            secondTouch = touch.location(in: drawingPlace)


            if(self.currentContext == nil)
            {
                UIGraphicsBeginImageContext(drawingPlace.frame.size)
                self.currentContext = UIGraphicsGetCurrentContext()
            }else{
                self.currentContext?.clear(CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height))
            }

            self.prevImage?.draw(in: self.drawingPlace.bounds)

            let bezier = UIBezierPath()

            bezier.move(to: startTouch!)
            bezier.addLine(to: secondTouch!)
            bezier.close()


            UIColor.blue.set()

            self.currentContext?.setLineWidth(4)
            self.currentContext?.addPath(bezier.cgPath)
            self.currentContext?.strokePath()
            let img2 = self.currentContext?.makeImage()
            drawingPlace.image = UIImage.init(cgImage: img2!)
        }
    }

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

        self.currentContext = nil
        self.prevImage = self.drawingPlace.image
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述