gpt4 book ai didi

UIView CGGradient 裁剪为绘制 "frame"

转载 作者:行者123 更新时间:2023-12-04 06:44:54 24 4
gpt4 key购买 nike

我正在尝试绘制更复杂的 UILabels(或 UIView,如果需要,将 UILabel 放在顶部)背景。由于我希望在用户更改 iPhone 方向时自动调整大小,因此我尝试在子类 drawRect 函数中实现它。如果可能的话,我希望能够在代码中调整这一切,省略对图案图像的需要。

我从一些以前关于这个主题的帖子中得到了一些提示:

Gradients on UIView and UILabels On iPhone

Make Background of UIView a Gradient Without Sub Classing

不幸的是,他们都没有达到目标,因为我想将渐变与自定义绘图结合起来。我希望 CGGradient 被我正在绘制的线框剪裁(在圆角处可见),但我无法做到这一点。

另一种方法是使用 CAGradientLayer,它似乎工作得很好,但这需要在旋转时对框架进行一些调整,并且无论我如何尝试,渐变层似乎都绘制在文本之上。

我的问题是双重的

  • 如何修改下面的代码以将渐变剪辑到绘制的“框架”
  • 如何让 CAGradientLayer 在 UILabel 的文本后面绘制(或者我是否必须将 UIView 放在 UILabel 后面,CAGradientLayer 作为背景)

  • 这是我的 drawrect 函数:
    - (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(c, [[UIColor clearColor] CGColor]);
    CGContextSetStrokeColorWithColor(c, [strokeColor CGColor]);
    CGContextSetLineWidth(c, 1);

    CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
    CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect) ;
    minx = minx + 1;
    miny = miny + 1;

    maxx = maxx - 1;
    maxy = maxy - 1;

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 0.6, 0.6, 0.6, 1.0, // Start color
    0.3, 0.3, 0.3, 1.0 }; // End color

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGRect currentBounds = [self bounds];
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGPoint lowCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));

    CGContextDrawLinearGradient(c, glossGradient, topCenter, midCenter, 0);

    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace);

    CGContextMoveToPoint(c, minx, midy);
    CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE_INFO);
    CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE_INFO);
    CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE_INFO);
    CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE_INFO);

    // Close the path
    CGContextClosePath(c);
    // Fill & stroke the path
    CGContextDrawPath(c, kCGPathFillStroke);
    // return;

    [super drawRect: rect];

    最佳答案

    你要找的是 CGContextClip()

    在您的代码中创建您的路径和渐变,然后

    CGContextClip(c);
    CGContextDrawLinearGradient(c, glossGradient, topCenter, midCenter, 0);
    CGGradientRelease(glossGradient);

    (删除您对 CGContextDrawLinearGradient 的旧调用)

    还要记住在进行任何剪辑之前和之后保存和恢复您的上下文

    如果您的剪辑与您想要的相反,请尝试 CGContextEOClip() (或以相反的顺序绘制路径)

    关于UIView CGGradient 裁剪为绘制 "frame",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3848955/

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