我正在尝试绘制渐变,不透明度渐渐消失。
我认为绘画没有效果。尝试改变颜色。框架看起来正确。为什么这种不透明度不会出现?
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 :(得分:0)
将v.layer.mask = l
替换为v.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)
}