iOS-mask遮罩

iOS-mask遮罩_第1张图片
heart.png

利用一张黑底透明的图片作为CALayer的mask,可以绘制出相同形状不同颜色的图形。

#import "ICEView.h"
@interface ICEView()
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) CALayer *iceLayer;
@property (nonatomic, strong) CAKeyframeAnimation *frameAnimate;

@property (nonatomic, assign) BOOL isSelected;
@end

@implementation ICEView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatBaseView];
        _isSelected = NO;
    }
    return self;
}

- (void)creatBaseView {
    _btn = [[UIButton alloc]initWithFrame:self.bounds];
    [self addSubview:_btn];
    
    _iceLayer = [CALayer layer];
    _iceLayer.frame = self.bounds;
    [_btn.layer addSublayer:_iceLayer];
    _iceLayer.mask = [CALayer layer];
}

- (void)layoutSubviews {
    _iceLayer.frame = _btn.bounds;

    _iceLayer.mask.frame = self.bounds;
    
    _iceLayer.backgroundColor = [UIColor lightGrayColor].CGColor;

    _iceLayer.mask.contents = (__bridge id _Nullable)([UIImage imageNamed:@"heart"].CGImage);
}
iOS-mask遮罩_第2张图片
IMG_1170.PNG

你可能感兴趣的:(iOS-mask遮罩)