2017-12-01 10 views
0

私は何でも私のCAShapeLayerの不透明度を変更することはできません。私は不透明度を設定しようとしました:CAShapeLayerアルファ/不透明度の値を変更するには?

bottomProgressBar.strokeColor = UIColor.white.cgColor 
    bottomProgressBar.opacity = 0.1 

動作しませんでした。その後、私はこれを試してみました:

bottomProgressBar.strokeColor = UIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 0.1).cgColor 

どちらの場合も、アルファ値は1
でこれを行うにはそこに別の方法ですか?あるいは私はそれらを間違って設定していますか?

編集:ここでは

が私のコードです:
これはUIViewから継承するクラスです:

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

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    configure() 
} 

func configure() { 
    bottomProgressBar.strokeColor = UIColor.white.cgColor 
    bottomProgressBar.opacity = 0.1 
    bottomProgressBar.lineWidth = self.frame.height 
    bottomProgressBar.strokeStart = 0 
    bottomProgressBar.strokeEnd = 1 
    self.layer.addSublayer(bottomProgressBar) 

    topProgressBar.strokeColor = UIColor.white.cgColor 
    topProgressBar.lineWidth = self.frame.height 
    topProgressBar.strokeStart = 0 
    topProgressBar.strokeEnd = 1 
    self.layer.addSublayer(topProgressBar) 
} 

編集2:

override func layoutSubviews() { 
    super.layoutSubviews() 

    let path = UIBezierPath() 
    path.move(to: CGPoint(x: 0, y: 0)) 
    path.addLine(to: CGPoint(x: self.frame.width, y: 0)) 

    bottomProgressBar.path = path.cgPath 
    topProgressBar.path = path.cgPath 
} 

が編集3:

enter image description here

+0

私はこれらの両方のソリューションをテストしましたが、どちらも私の仕事です。 – Brian

+0

正しいものを変更していますか – zombie

+0

ボトムビューとトップビューのプログレスバーがあり、ボトムビューのアルファ値は0.1に設定されていますが、常に1です。両方のビューのアルファ値は1に設定されています:/ – Kobe

答えて

0

私は、レンダリングされる図形のパスが定義されていないと考えています。

func configure() { 
     let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)) 
     bottomProgressBar.frame = self.frame 
     bottomProgressBar.bounds = self.bounds 
     bottomProgressBar.path = path.cgPath 
     bottomProgressBar.strokeColor = UIColor.red.cgColor 
     bottomProgressBar.opacity = 0.1 
//  no need for the following line as dimensions are already defined 
//  bottomProgressBar.lineWidth = self.frame.height 
//  bottomProgressBar.strokeStart = 0 
//  bottomProgressBar.strokeEnd = 200 
     self.layer.addSublayer(bottomProgressBar) 

     // path width divided by 2 for demo 
     let topPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width/2, height: self.frame.height)) 
     topProgressBar.frame = self.frame 
     topProgressBar.bounds = self.bounds 
     topProgressBar.path = topPath.cgPath 
     topProgressBar.strokeColor = UIColor.green.cgColor 
     self.layer.addSublayer(topProgressBar) 
    } 

これが役に立ちます。

+0

質問をより多くのコードで更新しました。私は実際にlayoutSubviews()にパスを設定しました。どのようにそれが大丈夫か分からない。 – Kobe

+0

その場合、元のコードが動作しています。あなたはそれがそうではないと思うのですか?あなたはスクリーンショットを供給できますか?編集:たとえば、 'topProgressBar.strokeEnd = 0.5'を設定すると、下線レイヤーに透明性があることがわかります。 – Brian

+0

完了。あなたが見ることができるように、底部が1ptの高さを有することだけが異なり、上部は2ptの高さを有する。 – Kobe

関連する問題