Reputation: 368
I have trying to draw using CALayer through subclassing . I know there are some posts regarding i have seen most of them and followed the solutions give but no success. like setting the frame size. Here is the code. Need some help thx in advance
//
// 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
and the NewLayer class
- (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);
}
Upvotes: 3
Views: 3830
Reputation: 696
The addCustomLayer
method never gets called. In doInitialSetup
a layer gets created but its type is CALayer
and not your custom subclass NewLayer
.
Upvotes: 3