如何创建UIBezierPath渐变填充? [英] How to create UIBezierPath gradient fill?

查看:155
本文介绍了如何创建UIBezierPath渐变填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用10px圆角和渐变填充创建 UIBezierPath 。我怎样才能实现这种效果?

I would like to create a UIBezierPath with 10px rounded corners and with gradient fill. How can I acheive this effect?

这是我想要做的图像:

如您所见,此广场有:


  • 2px黑色边框

  • 10px圆角

  • 红色到绿色线性渐变填充

如何以编程方式不使用图案图像颜色

以下是我创建路径的方法:

Here's how I create the path:

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


推荐答案

我能想到的3种方式。


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

  1. Create a CAGradientLayer and insert it as a sublayer of theView.layer. You can even put a rounded corner radius on the layer. Of course you'll have to import QuartzCore framework.

使用CoreGraphics这样做:

Do it with CoreGraphics, like so:

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


  • 创建一个像素宽且具有渐变的屏幕外图像上下文,生成然后使用colorWithPatternImage设置背景颜色。

  • Create an off-screen image context that's one pixel wide and has a gradient, generate the image, then set the background color with colorWithPatternImage.

    这些是最容易的,最难的。

    These are in order of easiest to hardest.

    这篇关于如何创建UIBezierPath渐变填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆