我有一个问题。我在网上寻找答案,但我不明白为什么不起作用。我一定是犯了一些我想不通的愚蠢错误。
如果我做:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
}
它会在屏幕上创建蓝色 View ,但如果我让
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
CAShapeLayer * layer = [[CAShapeLayer alloc]init];
layer.frame = CGRectMake(10, 10, 30, 30);
layer.fillColor = [[UIColor blackColor] CGColor];
view.layer.mask = layer;
}
它没有显示任何内容。
如果我理解正确,它应该屏蔽在 (10,10,30,30)?
最佳答案
您需要在 CAShapeLayer 上设置路径:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
CAShapeLayer * layer = [[CAShapeLayer alloc]init];
layer.frame = view.bounds;
layer.fillColor = [[UIColor blackColor] CGColor];
layer.path = CGPathCreateWithRect(CGRectMake(10, 10, 30, 30), NULL);
view.layer.mask = layer;
}
关于ios - CAShapeLayer mask View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20090038/