本文介绍了CAShapeLayer面具视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题。我在网上寻找答案,我不明白为什么不工作。我必须做一些我无法弄清楚的愚蠢错误。
I have one problem. I look for the answers on the net and I don't understand why is not working. I must do some stupid mistake which I can't figure it out.
如果我做的话:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
}
它在屏幕上创建蓝色视图,但如果我制作
it create blue view on the screen, but if I make
- (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;
}
它没有显示任何内容。
it don't show anything.
如果我理解正确,它应该掩盖(10,10,30,30)?
If I understand correctly it should mask at (10,10,30,30)?
推荐答案
您需要在CAShapeLayer上设置路径:
You need to set the path on the 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;
}
这篇关于CAShapeLayer面具视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!