【问题标题】:CAGradientLayer with Rounded Top Corners (without using .mask)带有圆角顶角的 CAGradientLayer(不使用 .mask)
【发布时间】:2012-06-16 05:17:26
【问题描述】:

我希望创建一个带圆角的CAGradientLayer。我不能使用.cornerRadius,因为我想只圆顶两个角。另外,我不能使用.mask,因为我想在之后对其进行截图,而renderInContext 不支持CALayer 掩码。

我该怎么做:

  • 答:创建一个带有渐变和两个圆角的CALayer,而不使用蒙版

  • B:截取类似于UIGraphicsGetImageFromCurrentImageContext 的屏幕截图,但尊重面具。

【问题讨论】:

  • 我认为您应该使图层透明并将图层内容设置为顶部圆角的图像。
  • 我希望以编程方式进行。这一定是可能的!
  • 我也想做同样的事情,你知道了吗?

标签: objective-c screenshot calayer quartz-core


【解决方案1】:

我想出了一种程序化的方法来做到这一点。它涉及创建一个圆形图层,然后在其上绘制一个方形图层。您必须对渐变颜色和位置进行一些调整才能获得看起来不错的东西。

注意:鉴于您对口罩的要求,不确定此解决方案是否适合您

+ (void)makeGradientButton:(UIButton *)button {
    // Create a rounded layer
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = button.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
    gradient.cornerRadius = 8.0f;

    // Now create a square layer that draws over the top
    CAGradientLayer *gradient2 = [CAGradientLayer layer];
    gradient2.frame = CGRectMake(0, 9, button.bounds.size.width, button.bounds.size.height-9);
    gradient2.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor redColor] CGColor], nil];
    gradient2.cornerRadius = 0.0f;

    [gradient setMasksToBounds:YES];
    [button.layer insertSublayer:gradient atIndex:0];
    [button.layer insertSublayer:gradient2 atIndex:1];
    [button setBackgroundColor:[UIColor clearColor]];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2021-05-14
    • 2023-04-01
    • 2013-05-15
    • 1970-01-01
    • 2011-11-09
    • 2022-01-12
    相关资源
    最近更新 更多