gpt4 book ai didi

objective-c - CAGradientLayer 作为 UIView 的 layerClass

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:03 26 4
gpt4 key购买 nike

我试图通过覆盖 UIView 的 layerClass 类方法,将 CAGradientLayer 用作 UIView 子类的根层。我为 UIButton 子类做了完全相同的事情,并且一切正常,但是对于 UIView,我没有看到渐变,我只看到默认的白色 View 背景。

这是我的代码:

#import <UIKit/UIKit.h>
#import "UIView+NBStyle.h"

@interface NBStylizedView : UIView

@property (nonatomic, strong) UIColor* highColor;
@property (nonatomic, strong) UIColor* lowColor;

@end

...

#import "NBStylizedView.h"
#import <QuartzCore/QuartzCore.h>

@implementation NBStylizedView

@synthesize highColor;
@synthesize lowColor;

+ (Class) layerClass {
return [CAGradientLayer class];
}

- (void)drawRect:(CGRect)rect {
if (self.highColor && self.lowColor) {
CAGradientLayer* gLayer = (CAGradientLayer*) self.layer;
[gLayer setColors:
[NSArray arrayWithObjects:
(id)[self.highColor CGColor],
(id)[self.lowColor CGColor], nil]];
}
[super drawRect:rect];
}

- (void)setHighColor:(UIColor*)color {
highColor = color;
[self.layer setNeedsDisplay];
}

- (void)setLowColor:(UIColor*)color {
lowColor = color;
[self.layer setNeedsDisplay];
}

@end

任何人都可以阐明我的问题所在吗?

最佳答案

我试过你的代码,一开始也没有看到渐变。如果我在 View 的 awakeFromNib 方法中将 View 的背景色设置为 clearColor,它就起作用了。

关于objective-c - CAGradientLayer 作为 UIView 的 layerClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com