Reputation: 25687
I'm trying to make the ends of my UIScrollView fade out - just like one would a UITableView. I found the below code. I'm using it in a UIScrollView subclass in a method called inside initWithFrame:...
.
if (!maskLayer)
{
maskLayer = [CAGradientLayer layer];
CGColorRef outerColor = [UIColor colorWithWhite:0 alpha:1.0].CGColor;
CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:0.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;
}
NOTE: I've already seen this question, but I already have __bridge
before my colors.
I'm sure it's something really basic, but I just can't figure it out. I added the CGColorRelease
lines in hope that would fix it, but it didn't...
Does anyone see anything I don't?
Upvotes: 0
Views: 193
Reputation: 495
Assuming you are setting up the scrollview in a XIB, drop the if statement and place the code into an awakeFromNib in your subclass. If manually creating the scroll view object, call that code after performing the inithWithFrame.
Also, flip the alpha values of the outerColor and innerColor variables. Here is the code.
- (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];
}
Upvotes: 1