在视图内的矩形内绘制简单的线性渐变

发布于 2024-10-16 19:57:06 字数 528 浏览 2 评论 0原文

我正在阅读有关渐变的文档,但我有点迷失了。 我有一个视图,在该视图内,我只想在一个矩形(小于视图)内从下到上绘制一个简单的黑色到灰色的线性渐变。 我怎样才能做到这一点而不需要子类化任何东西(我读过很多需要子类化视图的东西)?

我正在寻找一种方法来做到这一点,就像我在各种平台上所做的那样简单。类似于(无语言:-)):

blackcolor = MakeBlack();
whiteColor = MakeWhite();

startPoint = MakeStartPoint();
endPoint = MakeEndPoint();

onthisgraphicport = SetGraphicPort(self.view);
clippingRect = MakeClipRect();

DrawGradient(from:whiteColor, to:blackcolor, from:startPoint, to:endPoint, onthisgraphicport, intoThisRect:clippingRect);

谢谢您的帮助。

I'm reading the documentation about gradients and I'm a little bit lost.
I have a view, and inside that view, I just want to draw a simple black to gray linear gradient inside a rect (smaller than the view), from bottom to top.
How may I do that without subclassing anything (I've read many things that need to subclass the view)?

I'm searching a way to do this as simple as I've ever done on various platforms. Something like (language free :-) ) :

blackcolor = MakeBlack();
whiteColor = MakeWhite();

startPoint = MakeStartPoint();
endPoint = MakeEndPoint();

onthisgraphicport = SetGraphicPort(self.view);
clippingRect = MakeClipRect();

DrawGradient(from:whiteColor, to:blackcolor, from:startPoint, to:endPoint, onthisgraphicport, intoThisRect:clippingRect);

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-10-23 19:57:06

此代码片段可能会有所帮助。我前段时间在网上找到了这段代码。不幸的是,我忘记了这个网站,所以不知道该归功于谁。

代码按原样绘制白色到黑色的渐变。只需根据您的需要更改矩形和颜色即可。

@interface MyView : UIView {
    CGGradientRef _gradientRef;
}

@end

@implementation MyView

- (void) dealloc
{
    [super dealloc];
}

- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat colors[] =
        {
            1.0f, 1.0f, 1.0f, 1.0f,
            0.0f, 0.0f, 0.0f, 1.0f,
        };
        _gradientRef = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
        CGColorSpaceRelease(rgb);
    }

    return self;
}

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPoint start = rect.origin;
    start.y = 0;
    CGPoint end = CGPointMake(rect.origin.x, rect.size.height);
    CGContextDrawLinearGradient(context, _gradientRef, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

    [super drawRect:rect];
}

@end

This code snippet might help. I found the code some time ago on the web. Unfortunately, I forgot the site, so not sure whom to give credit.

The code as is draws a white to black gradient. Just change the rect and the colors to your needs.

@interface MyView : UIView {
    CGGradientRef _gradientRef;
}

@end

@implementation MyView

- (void) dealloc
{
    [super dealloc];
}

- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat colors[] =
        {
            1.0f, 1.0f, 1.0f, 1.0f,
            0.0f, 0.0f, 0.0f, 1.0f,
        };
        _gradientRef = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
        CGColorSpaceRelease(rgb);
    }

    return self;
}

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPoint start = rect.origin;
    start.y = 0;
    CGPoint end = CGPointMake(rect.origin.x, rect.size.height);
    CGContextDrawLinearGradient(context, _gradientRef, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

    [super drawRect:rect];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文