iOS-mask遮罩

2017-06-22  本文已影响974人  linbj
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);
}
IMG_1170.PNG
上一篇下一篇

猜你喜欢

热点阅读