美文网首页
iOS-mask遮罩

iOS-mask遮罩

作者: linbj | 来源:发表于2017-06-22 15:17 被阅读974次
    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

    相关文章

      网友评论

          本文标题:iOS-mask遮罩

          本文链接:https://www.haomeiwen.com/subject/wyjfcxtx.html