Created
January 11, 2018 20:24
-
-
Save unboxme/8c4b8c9850936b03c496e8226877528c to your computer and use it in GitHub Desktop.
Fading view edges
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
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