NSScrollview 与 NSGradient

发布于 2024-11-05 10:44:10 字数 1334 浏览 0 评论 0原文

我的应用程序中有一个 nsscroll 视图,我创建了 nsscrollview 的子类来添加 nsgradient,但它不起作用,这是我的实现文件中的代码:

#import "scrollview.h"
@implementation scrollview
@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        [self setStartingColor:[NSColor colorWithCalibratedRed:0.941 green:0.941 blue:0.941 alpha:1]];
        [self setEndingColor:[NSColor colorWithCalibratedRed:0.6588 green:0.6588 blue:0.6588 alpha:1]];


        [self setAngle:90];
    }
    return self;
}

- (void)drawRect:(NSRect)rect {

    NSBezierPath* roundRectPath = [NSBezierPath bezierPathWithRoundedRect: [self bounds] xRadius:10 yRadius:10]; 
    [roundRectPath addClip];
    if (endingColor == nil || [startingColor isEqual:endingColor]) {
        // Fill view with a standard background color
        [startingColor set];
        NSRectFill(rect);
    }
    else {
        // Fill view with a top-down gradient
        // from startingColor to endingColor
        NSGradient* aGradient = [[NSGradient alloc]
                                 initWithStartingColor:startingColor
                                 endingColor:endingColor];
        [aGradient drawInRect:[self bounds] angle:angle];
    }
}

I have a nsscroll view in my application and i made a subclass of nsscrollview to add a nsgradient but it doesn't work this is my code in my implementation file:

#import "scrollview.h"
@implementation scrollview
@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        [self setStartingColor:[NSColor colorWithCalibratedRed:0.941 green:0.941 blue:0.941 alpha:1]];
        [self setEndingColor:[NSColor colorWithCalibratedRed:0.6588 green:0.6588 blue:0.6588 alpha:1]];


        [self setAngle:90];
    }
    return self;
}

- (void)drawRect:(NSRect)rect {

    NSBezierPath* roundRectPath = [NSBezierPath bezierPathWithRoundedRect: [self bounds] xRadius:10 yRadius:10]; 
    [roundRectPath addClip];
    if (endingColor == nil || [startingColor isEqual:endingColor]) {
        // Fill view with a standard background color
        [startingColor set];
        NSRectFill(rect);
    }
    else {
        // Fill view with a top-down gradient
        // from startingColor to endingColor
        NSGradient* aGradient = [[NSGradient alloc]
                                 initWithStartingColor:startingColor
                                 endingColor:endingColor];
        [aGradient drawInRect:[self bounds] angle:angle];
    }
}

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

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

发布评论

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

评论(1

烂柯人 2024-11-12 10:44:10

第一步是创建一个绘制渐变的自定义 NSView 子类:

GradientBackgroundView.h:

@interface GradientBackgroundView : NSView
{}
@end

GradientBackgroundView.m:

#import "GradientBackgroundView.h"
@implementation GradientBackgroundView    
- (void) drawRect:(NSRect)dirtyRect
{
    NSGradient *gradient = [[[NSGradient alloc] initWithStartingColor:[NSColor redColor] endingColor:[NSColor greenColor]] autorelease];
    [gradient drawInRect:[self bounds] angle:90];
}
@end

下一步是使滚动视图的文档视图成为此类的实例(而不是普通的 NSView)。

在 IB 中,双击滚动视图,然后在“标识”窗格中将“类”设置为 GradientBackgroundView。

从这一点开始,事情就基本按照标准方式处理了。您可以向文档视图添加子视图、调整其大小等。这是一个屏幕截图:
渐变背景

The first step is to create a custom NSView subclass that draws a gradient:

GradientBackgroundView.h:

@interface GradientBackgroundView : NSView
{}
@end

GradientBackgroundView.m:

#import "GradientBackgroundView.h"
@implementation GradientBackgroundView    
- (void) drawRect:(NSRect)dirtyRect
{
    NSGradient *gradient = [[[NSGradient alloc] initWithStartingColor:[NSColor redColor] endingColor:[NSColor greenColor]] autorelease];
    [gradient drawInRect:[self bounds] angle:90];
}
@end

The next step is to make the scroll view's document view an instance of this class (instead of plain NSView).

In IB, double-click your scroll view, and in the Identity pane set the Class to GradientBackgroundView.

From this point on, things are handled pretty much in the standard way. You can add subviews to the document view, resize it, etc. Here's a screenshot:
Gradient background

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