我正在尝试创建两个渐变层,在滚动视图的每一侧各放置一个,以便显示有可滚动到的内容。
左侧的一个效果很好,但右侧的一个完全透明。我想念什么?
CAGradientLayer *l = [CAGradientLayer layer];
l.frame = self.leftBlur.frame;
l.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
l.startPoint = CGPointMake(0.0f, 0.5f);
l.endPoint = CGPointMake(1.0f, 0.5f);
self.leftBlur.layer.mask = l;
self.leftBlur.hidden = YES;
CAGradientLayer *r = [CAGradientLayer layer];
r.frame = self.rightBlur.frame;
r.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor whiteColor].CGColor, nil];
r.startPoint = CGPointMake(0.0f, 0.5f);
r.endPoint = CGPointMake(1.0f, 0.5f);
self.rightBlur.layer.mask = r;
调试输出:
(lldb) po r
<CAGradientLayer:0x170ba6d0; position = CGPoint (305 62.5); bounds = CGRect (0 0; 30 125); allowsGroupOpacity = YES; endPoint = CGPoint (1 0.5); startPoint = CGPoint (0 0.5); colors = (
"<CGColor 0x15e590e0> [<CGColorSpace 0x15e34b50> (kCGColorSpaceDeviceGray)] ( 1 1 )",
"<CGColor 0x17012ed0> [<CGColorSpace 0x15d15310> (kCGColorSpaceDeviceRGB)] ( 1 0 0 1 )"
)>
(lldb) po self.rightBlur
<UIImageView: 0x171377d0; frame = (290 0; 30 125); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17137860>>
(lldb) po l
<CAGradientLayer:0x170b9f00; position = CGPoint (15 62.5); bounds = CGRect (0 0; 30 125); allowsGroupOpacity = YES; endPoint = CGPoint (1 0.5); startPoint = CGPoint (0 0.5); colors = (
"<CGColor 0x15e590e0> [<CGColorSpace 0x15e34b50> (kCGColorSpaceDeviceGray)] ( 1 1 )",
"<CGColor 0x15d39320> [<CGColorSpace 0x15e34b50> (kCGColorSpaceDeviceGray)] ( 0 0 )"
)>
(lldb) po self.leftBlur
<UIImageView: 0x17136fc0; frame = (0 0; 30 125); hidden = YES; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17137050>>
最佳答案
将渐变层的框架设置为等于rightBlur
的框架。这将使rightBlur
中的层偏移。您应该将其设置为界限:
r.frame = self.rightBlur.bounds;
对于左侧,此方法有效,因为其原点是(0,0)
关于ios - CAGradientLayer显示完全透明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19074582/