2017-06-07 121 views
0

我想繪製一個漸變,在不透明狀態下消失。漸變蒙版不顯示

我看不出繪畫效果。試圖改變顏色。框架看起來是正確的。爲什麼不顯示這種不透明消失?

func createGradient() { 
     let h = UIScreen.mainScreen().bounds.size.height 
     let y = collectionView.frame.origin.y + (cellSize() * 3) 
     let v = UIView(frame: CGRect(x: 0, y: y, width: collectionView.frame.size.width, height: h - y)) 

     let l = CAGradientLayer() 
     l.frame = v.bounds 
     l.colors = [UIColor.greenColor().CGColor, UIColor.blueColor().CGColor] 

     l.startPoint = CGPoint(x: CGFloat(0.5), y: CGFloat(0.0)); 
     l.endPoint = CGPoint(x: CGFloat(0.5), y: CGFloat(1.0)); 
     v.layer.mask = l; 
     view.addSubview(v) 
    } 

我的日誌:

(lldb) po l 
<CAGradientLayer:0x618000228b00; position = CGPoint (207 101); bounds = CGRect (0 0; 414 202); allowsGroupOpacity = YES; colors = (
    "<CGColor 0x6180000b2c00> [<CGColorSpace 0x6100000376c0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] (0 1 0 1)", 
    "<CGColor 0x6180000b2c60> [<CGColorSpace 0x6100000376c0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] (0 0 1 1)" 
)> 

(lldb) po v 
<UIView: 0x7fe1b3d1d3b0; frame = (0 534; 414 202); layer = <CALayer: 0x618000228880>> 

(lldb) 

回答

0

代之以v.layer.mask = lv.layer.addSublayer(l)

func createGradient() { 
     let h = UIScreen.mainScreen().bounds.size.height 
     let y = collectionView.frame.origin.y + (cellSize() * 3) 
     let v = UIView(frame: CGRect(x: 0, y: y, width: collectionView.frame.size.width, height: h - y)) 

     let l = CAGradientLayer() 
     l.frame = v.bounds 
     l.colors = [UIColor.greenColor().CGColor, UIColor.blueColor().CGColor] 

     l.startPoint = CGPoint(x: CGFloat(0.5), y: CGFloat(0.0)); 
     l.endPoint = CGPoint(x: CGFloat(0.5), y: CGFloat(1.0)); 
     v.layer.addSublayer(l) 
     view.addSubview(v) 
    }