一、mask
An optional layer whose alpha channel is used to mask the layer’s content.
mask 也是一个layer层,通过它的alpha通道来控制layer层得显示。
CALayer *backLayer = [[CALayer alloc]init];
backLayer.frame = CGRectMake(0, 100, 300, 300);
backLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:backLayer];
CALayer *masklayer = [[CALayer alloc]init];
masklayer.frame = CGRectMake(20, 100, 100, 100);
masklayer.backgroundColor = [UIColor yellowColor].CGColor;
backLayer.mask = masklayer;
- 1、mask并不是我们意义上的遮盖层,它的alpha通道控制着所属layer显示。
- 2、masklayer必须是不透明的,这里设置masklayer的backgroundColor= [UIColor yellowColor].CGColor并不会真的会将layer渲染成黄色,这里的目的只是让masklayer是不透明的,因此你可以设置任意的颜色。默认masklayer是 clearColor,是透明的,而透明的mask,所属的layer是无法被渲染出来的。
- 3、layer默认的mask是nil,如果你给layer配置了mask,不要忘了设置masklayer的size和position。只有masklayer的frame内的layer才能被渲染出来,这有点裁剪的意思,也好似masklayer像一个窗户一样,layer只能透过来一部分内容被渲染出来。
二、渐变色进度条
// 进度条view
UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake(30.0, 100.0, self.view.bounds.size.width - 30.0 * 2, 5.0)];
progressView.center = (CGPoint){self.view.bounds.size.width / 2, self.view.bounds.size.height / 3};
progressView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
progressView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.3];
progressView.layer.cornerRadius = progressView.bounds.size.height / 2;
progressView.layer.masksToBounds = YES;
[self.view addSubview:progressView];
// 渐变色layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = progressView.bounds;
gradientLayer.colors = @[(__bridge id)[[UIColor greenColor] colorWithAlphaComponent:1.0].CGColor,
(__bridge id)[[UIColor blueColor] colorWithAlphaComponent:1.].CGColor,
(__bridge id)[[UIColor purpleColor] colorWithAlphaComponent:1.0].CGColor,];
gradientLayer.startPoint = (CGPoint){.0};
gradientLayer.endPoint = (CGPoint){1.0};
[progressView.layer addSublayer:gradientLayer];
// mask layer
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = (CGRect){.0, .0, .0, gradientLayer.bounds.size.height};
maskLayer.backgroundColor = [UIColor whiteColor].CGColor;
gradientLayer.mask = maskLayer;
// 设置变化规则
CGFloat deltaWidth = gradientLayer.bounds.size.width / 60;
[NSTimer scheduledTimerWithTimeInterval:.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
CGRect rect = maskLayer.bounds;
rect.size.width += deltaWidth;
maskLayer.frame = rect;
if (gradientLayer.bounds.size.width - maskLayer.bounds.size.width < deltaWidth) {
[timer invalidate];
}
}];
三 、渐变色的文字
UIView *contentView = [[UIView alloc]init];
[self.view addSubview:contentView];
contentView.backgroundColor = [UIColor redColor];
contentView.frame = CGRectMake(100, 100, 300, 300);
// 渐变色layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = contentView.bounds;
gradientLayer.colors = @[(__bridge id)[[UIColor greenColor] colorWithAlphaComponent:1.0].CGColor,
(__bridge id)[[UIColor blueColor] colorWithAlphaComponent:1.].CGColor,
(__bridge id)[[UIColor purpleColor] colorWithAlphaComponent:1.0].CGColor,];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
[contentView.layer addSublayer:gradientLayer];
UILabel *lable =[[UILabel alloc]init];
lable.text = @"测试测试测试测试测试测试测试";
lable.font = [UIFont systemFontOfSize:30];
contentView.maskView = lable;
lable.frame = contentView.bounds;
四、给View设置圆角
UIView *redView = [[UIView alloc]init];
[self.view addSubview:redView];
redView.frame = CGRectMake(100, 100, 100, 40);
redView.backgroundColor = [UIColor redColor];
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:redView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer*maskLayer = [[CAShapeLayer alloc]init];
maskLayer.path= path.CGPath;
redView.layer.mask = maskLayer;