Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // 三角形 ------------------------------------- 9 // UIBezierPath のインスタンス生成 10 let line = UIBezierPath(); 11 // 起点 12 line.move(to: CGPoint(x: 50, y: 160)); 13 // 帰着点 14 line.addLine(to: CGPoint(x: 200, y: 600)); 15 line.addLine(to: CGPoint(x: 300, y: 280)); 16 // ラインを結ぶ 17 line.close() 18 // 色の設定 19 UIColor.red.setStroke() 20 // ライン幅 21 line.lineWidth = 4 22 // 描画 23 line.stroke(); 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 // Dispose of any resources that can be recreated. 29 } 30 31} 32
実行エラーは以下の通りです
2018-06-06 17:10:00.063156+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063244+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063273+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063299+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063350+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063391+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063418+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063462+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063488+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2018-06-06 17:10:00.063514+0900 UIBezierPathTest[401:270598] [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
普段は、外側にUIViewクラスとして別に作って呼びます
Swift
1 // Screen Size の取得 2 let screenWidth = self.view.bounds.width 3 let screenHeight = self.view.bounds.height 4 5 let testDraw = TestDraw(frame: CGRect(x: 0, y: 0, 6 width: screenWidth, height: screenHeight)) 7 self.view.addSubview(testDraw) 8 9 } 10
こちらでは、エラーを起こさすに動きます。
UIViewControllerに直接描画することはできないでしょうか?
参考にしている資料はこれです
資料
回答1件
あなたの回答
tips
プレビュー