NSView 与 CAgradientlayer

发布于 2024-11-04 23:33:50 字数 1301 浏览 3 评论 0原文

你好 我有一个简单的应用程序,其中有一些自定义视图,我想使用 CAgradientlayer 在其中一些视图中添加渐变效果,但我的代码不能很好地工作,我的代码

gradientLayer.bounds = CGRectMake(0, 0, [customer_general_view bounds].size.width, [customer_general_view bounds].size.height);

CGColorRef color1 = CGColorCreateGenericRGB(0.98, 0.98, 0.98, 1);
CGColorRef color2 = CGColorCreateGenericRGB(1, 1, 1, 1);
NSNumber *stopOne     = [NSNumber numberWithFloat:0.00];
NSNumber *stopTwo     = [NSNumber numberWithFloat:0.02];
NSNumber *stopThree  = [NSNumber numberWithFloat:0.02];
NSNumber *stopFour    = [NSNumber numberWithFloat:0.50];
NSNumber *stopFive     = [NSNumber numberWithFloat:0.50];
NSNumber *stopSix       = [NSNumber numberWithFloat:0.95];
NSNumber *stopSeven  = [NSNumber numberWithFloat:1.00];
NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, stopFour, stopFive, stopSix, stopSeven, nil];


//Package the new color pair in an array (the format required for CAGradientLayer)
NSArray *colors = [NSArray arrayWithObjects:(id) color1, color2, nil];

[gradientLayer setColors:colors];
gradientLayer.locations = locations;


//Release the colors
CGColorRelease(color1);
CGColorRelease(color2);

[customer_general_view setLayer:gradientLayer];
[customer_general_view setWantsLayer:YES];

我不知道我是否遗漏了一些东西。

Hi
I have a simple application where I have a some custom views and I want to add a gradient effect in some of them with CAgradientlayer but my code doesn't work well, my code

gradientLayer.bounds = CGRectMake(0, 0, [customer_general_view bounds].size.width, [customer_general_view bounds].size.height);

CGColorRef color1 = CGColorCreateGenericRGB(0.98, 0.98, 0.98, 1);
CGColorRef color2 = CGColorCreateGenericRGB(1, 1, 1, 1);
NSNumber *stopOne     = [NSNumber numberWithFloat:0.00];
NSNumber *stopTwo     = [NSNumber numberWithFloat:0.02];
NSNumber *stopThree  = [NSNumber numberWithFloat:0.02];
NSNumber *stopFour    = [NSNumber numberWithFloat:0.50];
NSNumber *stopFive     = [NSNumber numberWithFloat:0.50];
NSNumber *stopSix       = [NSNumber numberWithFloat:0.95];
NSNumber *stopSeven  = [NSNumber numberWithFloat:1.00];
NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, stopFour, stopFive, stopSix, stopSeven, nil];


//Package the new color pair in an array (the format required for CAGradientLayer)
NSArray *colors = [NSArray arrayWithObjects:(id) color1, color2, nil];

[gradientLayer setColors:colors];
gradientLayer.locations = locations;


//Release the colors
CGColorRelease(color1);
CGColorRelease(color2);

[customer_general_view setLayer:gradientLayer];
[customer_general_view setWantsLayer:YES];

I don't know if i'm missing something.

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

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

发布评论

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

评论(1

提赋 2024-11-11 23:33:50

我解决了制作 NSView 子类的问题。在标题中我写道:

@interface test_gradient : NSView {
    NSColor *startingColor;
    NSColor *endingColor;
    int angle;
}
@property(nonatomic, retain) NSColor *startingColor;
@property(nonatomic, retain) NSColor *endingColor;
@property(assign) int angle;

@end

在实现中我写道:

@implementation test_gradient

@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

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


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

- (void)drawRect:(NSRect)rect {
    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];
    }
}

@end

但我无法解决 CAgradient 层的问题,如果有人有答案那就太好了

I solve my problem making a subclass of NSView. In the header I wrote:

@interface test_gradient : NSView {
    NSColor *startingColor;
    NSColor *endingColor;
    int angle;
}
@property(nonatomic, retain) NSColor *startingColor;
@property(nonatomic, retain) NSColor *endingColor;
@property(assign) int angle;

@end

And in the implementation I wrote:

@implementation test_gradient

@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

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


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

- (void)drawRect:(NSRect)rect {
    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];
    }
}

@end

But I couldn't solve the problem with the CAgradient layer if anyone has a answer for that it would be great

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