下面的代码工作但响应非常慢,我无法用快速手指移动绘制。我认为我的子视图“TestView”是它的一个原因,但它是我的应用程序的基本要求。
在HomeView.h文件中
IBOutlet UIImageView *imgDraw;
IBOutlet UIView *TestView;
@private
CGPoint currentPoint;
CGPoint previousPoint1;
CGPoint previousPoint2;
这里我使用TestView作为HomeView中的子视图在TestView中绘制,这里imgDraw存在于TestView中。
在HomeView.m文件中
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:TestView];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self.view];
currentPoint = [touch locationInView:self.view];
// calculate mid point.
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
UIGraphicsBeginImageContext(TestView.frame.size);
[imgDrawAlphaPad.image drawInRect:CGRectMake(0, 0, self.TestView.frame.size.width, self.TestView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
//NSLog(@"iPhone");
//CGContextSetLineWidth(UIGraphicsGetCurrentContext(), [appDelegate_iPhone.StrokeWidth floatValue]);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 0.9);
CGContextBeginPath(UIGraphicsGetCurrentContext());
//Code for smooth drawing.{#DR#-12/05/2012}
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
//CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}