我需要别人的帮助。我想用PNG文件绘制线条,可以逐个显示(有一些空格)以及用户手指移动。下面的图片将表明我真正想做的事情。
这是我的代码......
-(CGPoint)generateDrawingPt:(NSMutableArray*)pointsArray{
CGFloat brushDia = 20.0f;
int spacing;
int distance;
CGPoint drawnPoint;
CGPoint tempPoint;
CGPoint resetPoint;
for (int i=0; i<pointsArray.count-1; i++) {
NSValue *curPointValue = [pointsArray objectAtIndex:i];
CGPoint curPoint = curPointValue.CGPointValue;
NSValue *nextPointValue = [pointsArray objectAtIndex:i+1];
CGPoint nextPoint = nextPointValue.CGPointValue;
distance=(CGFloat)sqrtf((curPoint.x-nextPoint.x)*(curPoint.x-nextPoint.x)+(curPoint.y-nextPoint.y)*(curPoint.y-nextPoint.y));
CGFloat dX = (nextPoint.x-curPoint.x);
CGFloat dY = (nextPoint.y-curPoint.y);
if (dX==0||dY==0 ||distance<0.5*brushDia){
resetPoint=CGPointMake((nextPoint.x+brushDia), (nextPoint.y+brushDia));
tempPoint=resetPoint;
resetPoint=drawnPoint;
drawnPoint=tempPoint;
curPoint.x=drawnPoint.x;
curPoint.y=drawnPoint.y;
} else {
drawnPoint=CGPointMake((curPoint.x-brushDia/2), (curPoint.y-brushDia/2));
curPoint.x=drawnPoint.x;
curPoint.y=drawnPoint.y;
}
}
return drawnPoint;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped =YES;
CGPoint drawingPt;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
NSValue *locationValue = [NSValue valueWithCGPoint:currentPoint];
[MovingPathPoints addObject:locationValue];
drawingPt =[self generateDrawingPt:MovingPathPoints];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
UIImage * brushImagetexture = [UIImage imageNamed:@"brushImg.png"];
[brushImagetexture drawAtPoint:drawingPt blendMode:kCGBlendModeHardLight alpha:0.6f];
CGContextStrokePath(context);
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
previousPoint = currentPoint;
}
通过上面的代码,我得到了非常糟糕的结果。这些问题包括
- 有些PNG图像不符合我的手指路线。 - 而且,反应很慢。手指移动结束后将绘制PNG图像。
有人可以帮帮我吗?
提前感谢。