CAayer - (void)drawInContext:(CGContextRef)ctx 永远不会被调用
Posted
技术标签:
【中文标题】CAayer - (void)drawInContext:(CGContextRef)ctx 永远不会被调用【英文标题】:CAayer - (void)drawInContext:(CGContextRef)ctx is never called 【发布时间】:2013-03-09 15:04:00 【问题描述】:我试图通过子类化使用 CALayer 进行绘制。我知道有一些关于我已经看过其中大部分的帖子并遵循了给出的解决方案但没有成功。比如设置帧大小。这是代码。需要一些帮助提前谢谢
//
// NewView.m
// layerPractise
#import "NewView.h"
#import "QuartzCore/QuartzCore.h"
#import "NewLayer.h"
#import "NewLayer2.h"
@interface NewView()
// NSMutableArray *_normalizedValues;
@end
@implementation NewView
@synthesize _containerLayer;
@synthesize layer,shouldAddNewLayer;
-(void)doInitialSetup
self._containerLayer = [CALayer layer];
[_containerLayer setFrame:self.frame];
[self.layer addSublayer:self._containerLayer];
// [_containerLayer retain];
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
[self doInitialSetup];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
[self doInitialSetup];
return self;
- (void)addCustomLayer
NewLayer *newLayer = [NewLayer layer];
newLayer.frame = self.bounds;
[_containerLayer addSublayer:newLayer];
[newLayer setNeedsDisplay];
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
//- (void)drawRect:(CGRect)rect
//
// // Drawing code
//
@end
和 NewLayer 类
- (id)initWithLayer:(id)layer
self = [super initWithLayer:layer];
if (self)
return self;
- (id)init
self = [super init];
if (self)
self.frame = CGRectMake(0, 0, 500, 500);
self.delegate = self;
CGRect r = self.frame;
[self setNeedsDisplay];
return self;
+ (BOOL)needsDisplayForKey:(NSString *)key
[super needsDisplayForKey:key];
- (void)drawInContext:(CGContextRef)ctx
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 500, 800);
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextDrawPath(ctx, kCGPathStroke);
【问题讨论】:
【参考方案1】:addCustomLayer
方法永远不会被调用。在doInitialSetup
中创建了一个层,但它的类型是CALayer
,而不是您的自定义子类NewLayer
。
【讨论】:
以上是关于CAayer - (void)drawInContext:(CGContextRef)ctx 永远不会被调用的主要内容,如果未能解决你的问题,请参考以下文章
(void(*)(void)) &shellcode啥意思?
从 'void* (*)(int*)' 到 'void* (*)(void*)' 的无效转换