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