1. layer層 mask 遮罩效果
//漸變層 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = CGRectMake(0, 100, kWidth, kWidth); gradientLayer.colors = @[(__bridge id)[[UIColor redColor]colorWithAlphaComponent:0.4] .CGColor, (__bridge id)[UIColor clearColor].CGColor]; gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(1, 0); [self.view.layer addSublayer:gradientLayer]; // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)]; CAShapeLayer *layer = [CAShapeLayer layer]; layer.frame = self.view.bounds; layer.lineWidth = 5; layer.strokeColor = [UIColor redColor].CGColor; layer.fillColor = [UIColor redColor].CGColor; layer.path = path.CGPath; layer.lineCap = @"round"; // [gradientLayer addSublayer:layer]; gradientLayer.mask = layer;
2. maskView 實現局部透明效果
//0.相當於maskView 將自己"投影"到 view上, 注意層級關系, 實際並不是在'灰色'的view上滑動, 而是投影到了"灰色"的view上了
//1.設置了遮罩mask屬性后, 只顯示重疊部分
//2.可以通過改變遮罩的alpha和顏色實現透明、半透明的效果
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:self.view.bounds]; imageView1.image = [UIImage imageNamed:@"1"]; [self.view addSubview:imageView1]; TestView *view = [TestView new]; view.frame = self.view.bounds; view.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.9]; [self.view addSubview:view]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; imageView.image = [UIImage imageNamed:@"1"]; [self.view addSubview:imageView]; UIView *roundView = [[UIView alloc]initWithFrame: CGRectMake(50, 50, 100, 100)]; roundView.backgroundColor = [UIColor redColor]; _viewwww = roundView; imageView.maskView = roundView;