3
我想學習Core Animation來開發某個應用程序,但我需要繼承CALayer類,但是我正在努力讓圖層自己繪製。創建一個自定義的CALayer實現
我需要自定義CALayer具有一些額外的屬性和處理自定義事件(觸摸等),但從我開始執行的基本CALayer沒有繪製自己,任何人都可以告訴我我做錯了什麼?
我有一個 幻方
#import "MagicSquare.h"
@implementation MagicSquare
-(id) initWithLayer:(id)layer {
self = [super initWithLayer:layer];
self.bounds = CGRectMake(0, 0, 200, 200);
self.position = CGPointMake(10,10);
self.cornerRadius = 100;
self.borderColor = [UIColor redColor].CGColor;
self.borderWidth = 1.5;
return self;
}
- (void)drawInContext:(CGContextRef)theContext
{
NSLog(@"Drawing");
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
CGPathAddCurveToPoint(thePath,
NULL,
15.f,250.0f,
295.0f,250.0f,
295.0f,15.0f);
CGContextBeginPath(theContext);
CGContextAddPath(theContext, thePath);
CGContextSetLineWidth(theContext,
1.0);
CGContextSetStrokeColorWithColor(theContext,
[UIColor redColor].CGColor);
CGContextStrokePath(theContext);
CFRelease(thePath);
}
和這裏的如何,我想有它繪製主控制器
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MagicSquare *layer = [[MagicSquare alloc] initWithLayer:[CALayer layer]];
[self.view.layer addSublayer:layer];
}
這是不直接關係到你的問題(你解決),但你不應該使用' initWithLayer:'創建你的圖層,但只需調用不帶參數的'init'。 – amadour