objective-c - NSView 与 CAgradientlayer

标签 objective-c cocoa gradient nsview xcode3.2

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

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

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

最佳答案

我解决了制作 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 层解决这个问题,如果有人能找到答案那就太好了

关于objective-c - NSView 与 CAgradientlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871144/

相关文章:

objective-c - 将 NSProgressIndicator 固定到底部

css - CSS中的垂直重复水平渐变

html - 是否可以在 css 3 中制作带有渐变的框箭头

android - 如何在左右侧 View 上创建阴影?

ios - 如何在 iOS 应用程序中评论 instagram 提要

ios - 禁用背景触摸 SpriteKit iOS

objective-c - 无法将 NSSound 实例放入 NSMutableArray ion cocoa 中

带包的 Cocoa 应用程序框架

iOS 8 当键盘出现时将 UIView 向上移动 |问题

cocoa - NSAlert 开始SheetModalForWindow :completionHandler: