0

我需要有人帮助。我想用 PNG 文件画线,该文件可以随着用户手指移动一一显示(有一些空间)。下图将表明我真正想做的事情。

截屏

这是我的代码……</p>

-(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图像将在手指末端移动后绘制。

有人可以帮帮我吗?

预先感谢。

4

0 回答 0