我想在触摸屏幕时绘制一个矩形。如果我直接绘制而不需要触摸屏幕,则会绘制矩形。但是当我在触摸屏幕时这样做时,它不会被绘制。这是我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
touchBegan = YES;
UITouch *touch = [[event allTouches]anyObject];
startPoint = [touch locationInView:self];
}
- (void)drawRect:(CGRect)rect
{
ctx = UIGraphicsGetCurrentContext();
if (touchBegan)
{
CGRect jrect = CGRectMake(100, 100, 100, 100);
CGContextAddRect(ctx, jrect);
CGContextFillPath(ctx);
[self setNeedsDisplay];
}
}
为什么不画出来?