uibezierpathを使った際に背景色がデフォルトで黒になってしまうため、背景を白もしくは透明にしたいと思い以下のようなコードを書いたのですが、一瞬だけ白になってすぐ黒に戻ってしまいます。
原因が分かる方がいたら教えていただけるとありがたいです。
始めて使った機能なのでよくわかってないですがよろしくお願いします。
viewcontroller.swift
1import UIKit 2 3class ViewController2: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 button1.layer.cornerRadius = 10 9 // Screen Size の取得 10 11 let screenWidth = self.view.bounds.width 12 let screenHeight = self.view.bounds.height 13 14 let testDraw = ViewLine(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)) 15 self.view.addSubview(testDraw) 16 self.view.sendSubview(toBack: View_first) 17 self.view.bringSubview(toFront: button1) 18 testDraw.isOpaque = false // 不透明を false 19 testDraw.backgroundColor = UIColor(white:1,alpha:0) 20 self.view.addSubview(testDraw) 21 22 23 } 24 25 override func didReceiveMemoryWarning() { 26 super.didReceiveMemoryWarning() 27 // Dispose of any resources that can be recreated. 28 } 29 30} 31
viewline.swift
1import UIKit 2 3class ViewLine: UIView { 4 5 /* 6 // Only override draw() if you perform custom drawing. 7 // An empty implementation adversely affects performance during animation. 8 override func draw(_ rect: CGRect) { 9 // Drawing code 10 } 11 */ 12 let Width = Int( UIScreen.main.bounds.size.width); 13 let Height = Int( UIScreen.main.bounds.size.height); 14 15 override func draw(_ rect: CGRect) { 16 17 // 三角形 ------------------------------------- 18 // UIBezierPath のインスタンス生成 19 let line = UIBezierPath(); 20 // 起点 21 line.move(to: CGPoint(x: Width, y: 0)); 22 // 帰着点 23 line.addLine(to: CGPoint(x: 0, y: Height)); 24 25 // ラインを結ぶ 26 line.close() 27 // 色の設定 28 UIColor.red.setStroke() 29 // ライン幅 30 line.lineWidth = 4 31 // 描画 32 line.stroke(); 33 34 } 35} 36
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/23 18:49
2016/11/23 18:55