在swift 3.0中绘制线条

时间:2017-07-25 11:54:42

标签: ios swift swift3

我使用以下代码在Swift 3.0中无效 - 它在模拟器上给我一个空白屏幕 - 我不知道发生了什么。

import UIKit

class DrawExample: UIView {

        override func draw( _ rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()
        context!.setLineWidth(3.0)
        context!.setStrokeColor(UIColor.purple.cgColor)

        //make and invisible path first then we fill it in
        context!.move(to: CGPoint(x: 50, y: 60))
        context!.addLine(to: CGPoint(x: 250, y:320))
        context!.strokePath()
    }

2 个答案:

答案 0 :(得分:3)

使用以下内容更新您的课程:

import UIKit

class DrawExample: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw( _ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        context!.setLineWidth(3.0)
        context!.setStrokeColor(UIColor.purple.cgColor)

        //make and invisible path first then we fill it in
        context!.move(to: CGPoint(x: 50, y: 60))
        context!.addLine(to: CGPoint(x: 250, y:320))
        context!.strokePath()
    }
}

现在在视图控制器上创建一个实例并将其添加到视图中。检查以下代码。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let draw = DrawExample(frame: self.view.bounds)
        view.addSubview(draw)
    }    
}

答案 1 :(得分:0)

在Swift 4.1中绘制

let lastPoint = CGPoint.zero
let red: CGFloat = 0.0
let green: CGFloat = 0.0
let blue: CGFloat = 0.0
let brushWidth: CGFloat = 10.0
let opacity: CGFloat = 1.0
var isSwiping:Bool!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    isSwiping    = false
    if let touch = touches.first{
        lastPoint = touch.location(in: imgViewDraw)
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    isSwiping = true;
    if let touch = touches.first{
        let currentPoint = touch.location(in: imgViewDraw)
        UIGraphicsBeginImageContext(self.imgViewDraw.frame.size)
        self.imgViewDraw.image?.draw(in: CGRect(x:0, y:0,width:self.imgViewDraw.frame.size.width, height:self.imgViewDraw.frame.size.height))

        UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
        UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))

        UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
        UIGraphicsGetCurrentContext()?.setLineWidth(self.brushWidth)
        UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
        UIGraphicsGetCurrentContext()?.strokePath()


        self.imgViewDraw.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        lastPoint = currentPoint
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(!isSwiping) {
        // This is a single touch, draw a point
        UIGraphicsBeginImageContext(self.imgViewDraw.frame.size)
        self.imgViewDraw.image?.draw(in: CGRect(x:0, y:0,width:self.imgViewDraw.frame.size.width, height:self.imgViewDraw.frame.size.height))
        UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
        UIGraphicsGetCurrentContext()?.setLineWidth(self.brushWidth)

        UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
        UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
        UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
        UIGraphicsGetCurrentContext()?.strokePath()
        self.imgViewDraw.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}