Skip to content

Instantly share code, notes, and snippets.

@unboxme
Created January 11, 2018 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unboxme/8c4b8c9850936b03c496e8226877528c to your computer and use it in GitHub Desktop.
Save unboxme/8c4b8c9850936b03c496e8226877528c to your computer and use it in GitHub Desktop.
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