サブクラス化を通じて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);
}