首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSGradient的NSScrollview

使用NSGradient的NSScrollview
EN

Stack Overflow用户
提问于 2011-05-06 00:47:05
回答 1查看 1K关注 0票数 2

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

代码语言:javascript
复制
#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];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-14 01:04:49

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

GradientBackgroundView.h View.h:

代码语言:javascript
复制
@interface GradientBackgroundView : NSView
{}
@end

GradientBackgroundView.m View.m:

代码语言:javascript
复制
#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

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

在IB中,双击滚动视图,然后在Identity窗格中将Class设置为GradientBackgroundView。

从这一点开始,事情基本上是以标准的方式处理的。您可以向文档视图添加子视图,调整其大小等。以下是一个屏幕截图:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5901334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档