Skip to content

Instantly share code, notes, and snippets.

@eoghain
Last active October 12, 2015 13:38
Show Gist options
  • Save eoghain/4035396 to your computer and use it in GitHub Desktop.
Save eoghain/4035396 to your computer and use it in GitHub Desktop.
FaderView - A UIView to add an edge fade
//
// FaderView.h
//
// Created by Rob Booth on 11/6/12.
//
//
// Usage
// [[FaderView alloc] initWithFrame:CGRectZero color:[UIColor redColor] andDirection:FaderViewDirectionDown];
#import <UIKit/UIKit.h>
typedef enum {
FaderViewDirectionDown = 0,
FaderViewDirectionUp = 1,
FaderViewDirectionLeft = 2,
FaderViewDirectionRight = 3,
} FaderViewDirection;
@interface FaderView : UIView
- (id)initWithFrame:(CGRect)frame color:(UIColor *)color andDirection:(FaderViewDirection)direction;
@end
//
// FaderView.m
//
// Created by Rob Booth on 11/6/12.
//
//
#import "FaderView.h"
@implementation FaderView
+ (Class)layerClass
{
return [CAGradientLayer class];
}
- (id)initWithFrame:(CGRect)frame color:(UIColor *)color andDirection:(FaderViewDirection)direction
{
self = [super initWithFrame:frame];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth+UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
NSArray * colors = @[ (id)color.CGColor, (id)[color colorWithAlphaComponent:0.0].CGColor ];
CGPoint startPoint = CGPointMake(0.5, 0.0);
CGPoint endPoint = CGPointMake(0.5, 1.0);
if (direction == FaderViewDirectionUp || direction == FaderViewDirectionLeft)
{
colors = @[ (id)[color colorWithAlphaComponent:0.0].CGColor, (id)color.CGColor ];
}
// Rotate start and end points
if (direction == FaderViewDirectionLeft || direction == FaderViewDirectionRight)
{
startPoint = CGPointMake(0.0, 0.5);
endPoint = CGPointMake(1.0, 0.5);
}
CAGradientLayer * layer = (id)self.layer;
layer.startPoint = startPoint;
layer.endPoint = endPoint;
layer.shouldRasterize = YES;
layer.colors = colors;
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment