Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Fading view edges
IB_DESIGNABLE
@interface FadingView ()
@property (assign, nonatomic) IBInspectable BOOL verticalFade;
@property (assign, nonatomic) IBInspectable BOOL horizontalFade;
@end
@implementation FadingView
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.opaque = NO;
self.backgroundColor = UIColor.clearColor;
if (!self.layer.mask && (self.verticalFade || self.horizontalFade)) {
NSArray *colors = @[
(id) UIColor.clearColor.CGColor,
(id) UIColor.whiteColor.CGColor,
(id) UIColor.whiteColor.CGColor,
(id) UIColor.clearColor.CGColor
];
CAGradientLayer *gradient = [[CAGradientLayer alloc] init];
gradient.locations = @[@0, @0.05, @0.95, @1];
gradient.colors = colors;
gradient.frame = self.bounds;
if (self.horizontalFade) {
gradient.frame = self.bounds;
gradient.startPoint = CGPointZero;
gradient.endPoint = CGPointMake(1, 0);
}
self.layer.mask = gradient;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment