Last active
October 12, 2015 13:38
-
-
Save eoghain/4035396 to your computer and use it in GitHub Desktop.
FaderView - A UIView to add an edge fade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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