gpt4 book ai didi

ios - CAShapeLayer mask View

转载 作者:行者123 更新时间:2023-12-01 17:54:40 25 4
gpt4 key购买 nike

我有一个问题。我在网上寻找答案,但我不明白为什么不起作用。我一定是犯了一些我想不通的愚蠢错误。

如果我做:

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com