<分区>
我将首先向您展示我需要的最终产品的外观。
我正在使用一个名为 BAFluidView 的椰子足类动物,它基本上可以模拟容器中流体的运动。开发商提供了一个 guide (请参阅“用作图层”部分)展示如何将 mask 添加到 fluidView 的图层以获得效果。
到目前为止,我可以使用添加到项目中的任何 UIImage 来屏蔽 fluiview。但是,我在尝试在水滴轮廓周围添加白色边框时遇到了问题,我可以寻求任何帮助。
非常感谢!
最佳答案
这就是我所说的“蛮力”方法。创建一个图像用作蒙版并创建第二个图像用作轮廓。
注意:这些图像有 alpha channel ,因此除非/直到您下载它们,否则可能看不清楚。棋盘图像显示了它们在 GIMP 中的外观。
mask 图像(我从 BAFluidView 示例中获取):
白色轮廓图像(相信我,它就在这里......只需点击下面):
和代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// load mask and outline
UIImage *maskingImage = [UIImage imageNamed:@"blueDrop"];
UIImage *outlineImage = [UIImage imageNamed:@"whiteOutlineThin"];
// define rect equal to size of mask image
CGRect rfv = CGRectMake(0, 0, maskingImage.size.width, maskingImage.size.height);
// instantiate BAFluidView
BAFluidView *fluidView = [[BAFluidView alloc] initWithFrame:rfv startElevation:@0.3];
fluidView.fillColor = [UIColor colorWithHex:0x092eee];
[fluidView fillTo:@0.90];
[fluidView startAnimation];
// if you want the "droplet" filled with a color
// fluidView.backgroundColor = [UIColor yellowColor];
// instantiate a couple CALayer objects
CALayer *maskingLayer = [CALayer layer];
CALayer *outlineLayer = [CALayer layer];
// set size to match mask
maskingLayer.frame = rfv;
outlineLayer.frame = rfv;
// set mask layer content to mask image
[maskingLayer setContents:(id)[maskingImage CGImage]];
// give the mask layer to BAFluidView
[fluidView.layer setMask:maskingLayer];
// set outline layer content to outline image
[outlineLayer setContents:(id)[outlineImage CGImage]];
// create a "container" view
UIView *containerView = [[UIView alloc] initWithFrame:rfv];
// add the outline layer
[containerView.layer addSublayer:outlineLayer];
// add the BAFluidView
[containerView addSubview:fluidView];
// add the container view to the screen / main view
[self.view addSubview:containerView];
// position the view with constraints...
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[containerView.widthAnchor constraintEqualToConstant:rfv.size.width].active = YES;
[containerView.heightAnchor constraintEqualToConstant:rfv.size.height].active = YES;
[containerView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[containerView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
}
结果截图:
您可以将其自动化,并使过程更加“优雅”和灵活,方法是仅使用蒙版图像并通过代码即时生成轮廓 - 将蒙版图像放大一点,然后例如,使用原始大小的图像对其进行 mask 。
关于ios - CALayer 蒙版周围的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144625/