2017-07-25 14 views
1

Swift 3.0で以下のコードを使用しないでください。私はシミュレータ上に空白の画面を表示します。何が起こっているのか分かりません。スウィフトで線を描く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. 
    } 


}