CAayer - (void)drawInContext:(CGContextRef)从不调用ctx

时间:2013-03-09 15:04:01

标签: ios uiview calayer

我试图通过子类化使用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 个答案:

答案 0 :(得分:3)

永远不会调用addCustomLayer方法。在doInitialSetup中,会创建一个图层,但其类型为CALayer,而不是您的自定义子类NewLayer