假设您在XIB中设置滚动视图,则删除if语句,并将代码放入子类中的wakeFromNib中。如果手动创建滚动视图对象,请在执行inithWithFrame之后调用该代码。
此外,翻转outerColor和innerColor变量的alpha值。这是代码。
- (void) setGradient
{
CAGradientLayer * maskLayer = [CAGradientLayer layer];
// Flipped your alpha values around.
CGColorRef outerColor = [UIColor colorWithWhite:0 alpha:0.0].CGColor;
CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
maskLayer.colors = [NSArray arrayWithObjects:(__bridge id)outerColor,
(__bridge id)innerColor, (__bridge id)innerColor, (__bridge id)outerColor, nil];
maskLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.8],
[NSNumber numberWithFloat:1.0], nil];
maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
maskLayer.anchorPoint = CGPointZero;
CGColorRelease(outerColor);
CGColorRelease(innerColor);
self.layer.mask = maskLayer;
}
- (void)awakeFromNib
{
[self setGradient];
}