1

I'm making a painting application in iOS . I'll give a brief outline of the logic here ,

1.Initialize a bitmap
2.Whenever a touchBegan or touchMoved event is detected , I draw a filled CGRect at that position on the bitmap and the bitmap is drawn to screen.

It works perfectly except for a little problem . If I move the mouse in the simulator really fast I dont get a continuous stroke . Its just a series of disconnected CGRects . I did some research which showed that in iOS touch events are fired only every 16 milliseconds but I dont think I'm moving the mouse that fast . So is the problem with the simulator or my code ?

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

       CGPoint touchPoint=[touch locationInView:self];

       CGContextFillRect (currentBitmap, CGRectMake (touchPoint.x -8 , touchPoint.y - 8   , rectangularBrushSize, rectangularBrushSize ));

       [self setNeedsDisplay]; //draw currentBitmap to screen

}

The code for touchesMoved is the same . The draw rect method draws currentBitmap to the screen .I'd like to add once more that it works perfectly when I move the mouse with medium speed.

4

2 回答 2

2

您应该尝试使用CGContextMoveToPointCGContextAddLineToPoint制作绘图应用程序。否则您的线条将看起来不流畅。

*我用这种方式写了一个应用程序,没有遇到你遇到的问题。

于 2012-09-07T09:54:14.657 回答
1

这很正常。您的代码或模拟器可能没有问题。不必在当前鼠标/手指位置绘制矩形,您必须记住(在实例变量中)最后一个位置并用一条线将其与当前位置连接起来。

touchesBegan

lastPosition = [[touches anyObject] locationInView:self];

touchesMoved

currentPosition = [[touches anyObject] locationInView:self];
CGContextMoveToPoint(context, lastPosition);
CGContextAddLineToPoint(context, currentPosition);
// Draw the line with the desired color and width here.
lastPosition = currentPosition;
于 2012-09-07T09:51:20.077 回答