4

我想创建一个UIBezierPath10px 圆角和渐变填充。我怎样才能达到这种效果?

这是我想要做的图像:

在此处输入图像描述

如您所见,这个广场有:

  • 2px黑色边框
  • 10px圆角
  • 红到绿线性渐变填充

如何在不使用图案图像颜色的情况下以编程方式执行此操作?

这是我创建路径的方式:

UIBezierPath *border = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0f];
[border setLineWidth:2];
[[UIColor blackColor] setStroke];
[border stroke];
[[UIColor redColor] setFill]; <-- what should I put here?
[border fill];
4

1 回答 1

4

我能想到的3种方法。

  1. 创建一个 CAGradientLayer 并将其作为 theView.layer 的子层插入。您甚至可以在图层上放置一个圆角半径。当然,您必须导入 QuartzCore 框架。

  2. 使用 CoreGraphics 执行此操作,如下所示:

    CGGradientRef gradient;
    CGColorSpaceRef colorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0,  // Start color
            0.0, 1.0, 0.0, 1.0 }; // End color
    
    colorspace = CGColorSpaceCreateDeviceRGB();
    gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
    CGContextDrawLinearGradient (ctx, gradient, gradientStartPoint, gradientEndPoint, 0);
    CGGradientRelease(gradient);
    
  3. 创建一个像素宽且具有渐变的屏幕外图像上下文,生成图像,然后使用 colorWithPatternImage 设置背景颜色。

这些按从易到难的顺序排列。

于 2012-02-12T16:11:55.920 回答