2017-07-25 34 views
1

我用下面的代码在Swift 3.0中无济于事 - 它给了我一个模拟器上的空白屏幕 - 我不知道发生了什么。在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() 
    } 
+0

的可能的复制[如何绘制在夫特3的线(https://stackoverflow.com/questions/40556112/how-to-draw-a-line-in-swift-3) – Krunal

回答

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

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


}