ios - CALayer 蒙版周围的边框

标签 ios iphone graphics calayer

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 6 年前

我将首先向您展示我需要的最终产品的外观。

Final Product

我正在使用一个名为 BAFluidView 的椰子足类动物,它基本上可以模拟容器中流体的运动。开发商提供了一个 guide (请参阅“用作图层”部分)展示如何将 mask 添加到 fluidView 的图层以获得效果。

到目前为止,我可以使用添加到项目中的任何 UIImage 来屏蔽 fluiview。但是,我在尝试在水滴轮廓周围添加白色边框时遇到了问题,我可以寻求任何帮助。

非常感谢!

最佳答案

这就是我所说的“蛮力”方法。创建一个图像用作蒙版并创建第二个图像用作轮廓。

注意:这些图像有 alpha channel ,因此除非/直到您下载它们,否则可能看不清楚。棋盘图像显示了它们在 GIMP 中的外观。

mask 图像(我从 BAFluidView 示例中获取):

enter image description here - enter image description here

白色轮廓图像(相信我,它就在这里......只需点击下面):

enter image description here - enter image description here

和代码:

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

}

结果截图:

enter image description here

您可以将其自动化,并使过程更加“优雅”和灵活,方法是仅使用蒙版图像并通过代码即时生成轮廓 - 将蒙版图像放大一点,然后例如,使用原始大小的图像对其进行 mask 。

关于ios - CALayer 蒙版周围的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144625/

上一篇:ios - Cordova-推送通知 firebase 未出现在状态栏中,但在 android 中工作

下一篇:iOS - 在 swift 3 中打印和存储字典数组

相关文章:

iOS: Realm ,检查对象是否为零

ios - 在自定义 UITableView 中设置委托(delegate)和数据源

javascript - 哪个 JavaScript 图形库的性能最好?

graphics - 创建 2D 图形资源的最佳实践

ios - UITableViewCell 未在 "Live"中自动调整大小

ios - 如何在iOS中集成Viber?

ios - 模态视图关闭时如何获得准确的 PresentingViewController

iphone - 计时器未正确倒计时

macos - 在 OS X 中,什么是 BASE 图形绘制层?

ios - 在 Swift 中同时检查集合的 nil 和empty是否安全?