0
我有一個UIView類,我正在使用CALayer。基於觸摸,此圖層將用於繪製線條。iPhone - 試圖在CALayer上畫線
這是類是如何定義的:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self == nil) {
return nil;
}
self.layer.backgroundColor = [UIColor redColor].CGColor;
self.userInteractionEnabled = YES;
path = CGPathCreateMutable();
return self;
}
然後我對的touchesBegan,TouchesMoved和touchesEnded以下行...
**touchesBegan**
CGPathMoveToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];
**touchesMoved**
CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];
**touchesEnded**
CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];
然後我有這個
-(void)drawInContext:(CGContextRef)context {
CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
CGContextBeginPath(context);
CGContextAddPath(context, path);
CGContextStrokePath(context);
}
touchesBegan/Moved/Ended方法被調用,但這個drawInContext方法永遠不會被調用d ...
我失蹤了什麼?
謝謝。
男人,你是一個天才!我已更正您的答案中的錯字,現在它正在完美工作。謝謝!!!! – SpaceDog 2011-06-04 04:56:53