Created
June 13, 2019 08:26
-
-
Save qy1010/0e4d71d008c1547683dbe7ec5374b626 to your computer and use it in GitHub Desktop.
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
#import <UIKit/UIKit.h> | |
NS_ASSUME_NONNULL_BEGIN | |
//渐变蒙层 | |
@interface HTVGradientLayer : UIView | |
@end | |
NS_ASSUME_NONNULL_END |
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
#import "HTVGradientLayer.h" | |
@interface HTVGradientLayer () | |
@property (nonatomic, strong) CAGradientLayer *gradientLayer; | |
@end | |
@implementation HTVGradientLayer | |
- (instancetype)init | |
{ | |
self = [super init]; | |
if (self) { | |
[self initLayerMask]; | |
} | |
return self; | |
} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self initLayerMask]; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
[self initLayerMask]; | |
} | |
return self; | |
} | |
- (void)initLayerMask | |
{ | |
self.layer.mask = self.gradientLayer; | |
} | |
- (CAGradientLayer *)gradientLayer | |
{ | |
if (_gradientLayer == nil) { | |
_gradientLayer = [[CAGradientLayer alloc] init]; | |
_gradientLayer.colors = @[(__bridge id)[[UIColor blackColor] colorWithAlphaComponent:0.2].CGColor,(__bridge id)[UIColor blackColor].CGColor]; | |
_gradientLayer.locations = @[@0.0, @0.2,@1.0]; | |
} | |
return _gradientLayer; | |
} | |
- (void)layoutSublayersOfLayer:(CALayer *)layer | |
{ | |
[super layoutSublayersOfLayer:layer]; | |
_gradientLayer.frame = self.bounds; | |
} | |
- (void)layoutSubviews | |
{ | |
[super layoutSubviews]; | |
[CATransaction begin]; | |
[CATransaction setDisableActions:YES]; | |
self.gradientLayer.frame = self.bounds; | |
[CATransaction commit]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment