1

我想在触摸屏幕时绘制一个矩形。如果我直接绘制而不需要触摸屏幕,则会绘制矩形。但是当我在触摸屏幕时这样做时,它不会被绘制。这是我的代码:

- (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];
}

}

为什么不画出来?

4

1 回答 1

0

您必须调用 drawrect:(Cgrect)rect 来绘制您的矩形 ex:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

[self drawrect:(Cgrect)rect];


}
于 2015-01-07T17:05:15.630 回答