Ios 多重打孔式面具?
我以前做过简单的CALayer面具,但我想我对它们的用途感到困惑。我正在尝试用几个(2)视图来产生一种打孔效果 这是我到目前为止所拥有的。我希望有一个带有打孔标签和图像的白色正方形(这样你可以通过它看到棕色背景。我错在哪里Ios 多重打孔式面具?,ios,calayer,mask,Ios,Calayer,Mask,我以前做过简单的CALayer面具,但我想我对它们的用途感到困惑。我正在尝试用几个(2)视图来产生一种打孔效果 这是我到目前为止所拥有的。我希望有一个带有打孔标签和图像的白色正方形(这样你可以通过它看到棕色背景。我错在哪里 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor brownColor]; self.viewToPunch = [[UIView allo
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
self.viewToPunch = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.viewToPunch];
self.viewToPunch.backgroundColor = [UIColor whiteColor];
self.punchLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.punchLabel.textColor = [UIColor blackColor];
self.punchLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.punchLabel.text = @"punch";
self.punchLabel.textAlignment = NSTextAlignmentCenter;
self.punchImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"plus"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
[self.punchImage setContentMode:UIViewContentModeCenter];
self.viewsToPunch = @[self.punchLabel,self.punchImage];
[self punch:self.viewToPunch withUIViews:self.viewsToPunch];
}
- (void)punch:(UIView *) viewToPunch withUIViews:(NSArray *)viewsToPunch
{
CALayer *punchMask = [CALayer layer];
punchMask.frame = viewToPunch.frame;
NSMutableArray *sublayers = [[NSMutableArray alloc] init];
for (UIView *views in viewsToPunch){
[sublayers addObject:views.layer];
}
punchMask.sublayers = sublayers;
punchMask.masksToBounds = YES;
viewToPunch.layer.mask = punchMask;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.viewToPunch.frame = CGRectMake(50, 50, 100, 100);
self.punchLabel.frame = CGRectMake(0, 0, 100, 100);
self.punchImage.frame = CGRectMake(0, 0, self.viewToPunch.frame.size.width, 40.);
[self punch:self.viewToPunch withUIViews:self.viewsToPunch];
}
因此,不仅画面看起来是关闭的,而且似乎是打孔的反面。我如何反转面具并固定画面
非常感谢您的帮助!我将它放入了一个方法punch:withUIViews:中,这样我就有希望在其他区域重复使用它。当您将遮罩应用到
CALayer
时,它只会在遮罩不透明的部分绘制。但您只是应用了一个空的(透明的)遮罩,坐标不正确(这就是为什么视图不是完全透明的:遮罩没有完全覆盖视图;它应该是punchMask.frame=viewToPunch.bounds;
)
您可能需要查看CAShapeLayer
并为其指定路径。将其用作遮罩层
例如,请参见或。我尝试将CAGradationLayer和CAShapeLayer的掩码组合起来,这是可能的
你能在问题中写下你认为遮罩是什么以及它是如何工作的吗?还有,你为什么要将帧设置为零矩形?这就是我的编码方式-所有帧都是cgrectzero,我在视图中进行调整大小将使用LayoutSubview或LayoutSubview(因此,动画、帧更改等都是自动处理的。我认为CALayer遮罩的工作方式是逐像素检查遮罩上的alpha通道,并通过遮罩上的alpha值调整另一个视图上的alpha。哦,好吧,似乎这不是不可能的。我如何反转遮罩,使其产生打孔效果,而不是我所看到的效果。)有没有?嗯,在重新阅读您想要实现的内容后(带文本剪切的立体视图),我认为使用纯CoreAnimation很难做到这一点,因为你不能反转层的alpha,也不能使用透明文本颜色和
CATextLayer
动态创建必要的遮罩…通过自己绘制并使用它可能更容易做到这一点。另请参阅。