Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Faded Gradient on left/right sides of UICollectionView

I have a horizontal-scrolling UICollectionView within the main view of a view controller like so (Grey is UIView, Wood is UICollectionView):

I want to add fixed faded gradients on the far left & far right sies of this UICollectionView so that the scrolls appear to vanish as the user scrolls. How would I go about doing this? Does it involve some use of CAGradientLayer? I would be grateful for any help you can give me!

like image 891
Nate Kimball Avatar asked May 19 '13 06:05

Nate Kimball


2 Answers

I actually managed to figure this out using one mask layer thanks to this tutorial at cocoanetics. Here's what I did:

@interface ScalesViewController : UIViewController
{
    CAGradientLayer *maskLayer;
}
@end

Then in the .m, I placed in the following:

- (void)viewDidAppear:(BOOL)animated
{

    [super viewWillAppear: animated];

    if (!maskLayer)
    {
        maskLayer = [CAGradientLayer layer];

        CGColorRef outerColor = [[UIColor colorWithWhite:0.0 alpha:1.0] CGColor];
        CGColorRef innerColor = [[UIColor colorWithWhite:0.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.125],
                              [NSNumber numberWithFloat:0.875],
                              [NSNumber numberWithFloat:1.0], nil];

        [maskLayer setStartPoint:CGPointMake(0, 0.5)];
        [maskLayer setEndPoint:CGPointMake(1, 0.5)];

        maskLayer.bounds = self.mainCollectionView.bounds;
        maskLayer.anchorPoint = CGPointZero;

        [self.mainCollectionView.layer insertSublayer: maskLayer atIndex: 0];
    }
}

This creates a nice "fade to black" effect on both sides of my collection view. More colors can be added to the locations & color properties to refine the gradient blend. The start/endpoints determine the direction and location of the gradient.

like image 173
Nate Kimball Avatar answered Oct 05 '22 16:10

Nate Kimball


Try to add two layers of CAGradientLayer on the collection view sublayer:

#import <QuartzCore/QuartzCore.h>

CAGradientLayer *leftShadow = [CAGradientLayer layer];
leftShadow.frame = CGRectMake(0, 0, 100, self.collectionView.frame.size.height);
leftShadow.startPoint = CGPointMake(0, 0.5);
leftShadow.endPoint = CGPointMake(1.0, 0.5);
leftShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];

[self.collectionView.layer addSublayer:leftShadow];

CAGradientLayer *rightShadow = [CAGradientLayer layer];
rightShadow.frame = CGRectMake(CGRectGetWidth(self.collectionView.frame)-100.0, 0, 100, self.collectionView.frame.size.height);
rightShadow.startPoint = CGPointMake(1.0, 0.5);
rightShadow.endPoint = CGPointMake(0, 0.5);
rightShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];

[self.collectionView.layer addSublayer:rightShadow];
like image 32
Valent Richie Avatar answered Oct 05 '22 16:10

Valent Richie